Sicily 1781. Knight

Source: Internet
Author: User

1781. Knight Constraints

Time limit:1 secs, Memory limit:64 MB

Description

Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from anoth Er. The possible knight moves is shown in Figure 1.


Figure 1 Possible Knight moves on the board

Input

The first line contains an integer T (≤10), indicating the number of test cases.

In every case, the first line contains an integerN(≤500), indicating the size of the chess board (the entire board has a sizeN xN). The second and third line contain pair of integers (SRCR, SrcC), and (Dstr, DSTC), specifying the starting and ending Posi tion of the Knight on the Board (0≤SRCR, SrcC, Dstr, dstc≤N–1).

Output

For every case, the output of the minimum distance on a. If starting point and ending point was equal, distance is 0. If the knight can ' t reach the ending point, distance is-1.

Sample Input
21st
0 00 0100) 18 9
Sample Output
06


Knight Parade problem, bfs:0.01s

#include <stdio.h> #include <queue> #include <string.h>using namespace Std;int n;bool vis[505][505];    struct point {int II; int JJ;};        Point SP, Ep;int BFs () {if (Sp.ii = = Ep.ii && SP.JJ = = EP.JJ) return 0;    Queue<point> Q;    Memset (Vis, false, sizeof (VIS));    Q.push (SP);    Point temp, Temp_next;        int step = 0;        while (!q.empty ()) {step++;        int size = Q.size ();            while (size--) {temp = Q.front ();                        Q.pop (); if (Temp.ii > 1 && temp.jj < n-1 &&!VIS[TEMP.II-2][TEMP.JJ + 1]) {TEMP_NEXT.II                = Temp.ii-2;                TEMP_NEXT.JJ = temp.jj + 1;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next); } if (Temp.ii > 0 &&TEMP.JJ < n-2 &&!VIS[TEMP.II-1][TEMP.JJ + 2]) {TEMP_NEXT.II = temp.ii-1;                TEMP_NEXT.JJ = Temp.jj + 2;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                 } if (Temp.ii < n-1 && Temp.jj < n-2 &&!vis[temp.ii + 1][TEMP.JJ + 2]) {                TEMP_NEXT.II = Temp.ii + 1;                TEMP_NEXT.JJ = Temp.jj + 2;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                 } if (Temp.ii < n-2 && Temp.jj < n-1 &&!VIS[TEMP.II + 2][TEMP.JJ + 1]) {                TEMP_NEXT.II = Temp.ii + 2; Temp_nexT.JJ = temp.jj + 1;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                } if (Temp.ii < n-2 && temp.jj > 0 &&!VIS[TEMP.II + 2][temp.jj-1]) {                TEMP_NEXT.II = Temp.ii + 2;                TEMP_NEXT.JJ = temp.jj-1;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                } if (Temp.ii < n-1 && temp.jj > 1 &&!vis[temp.ii + 1][temp.jj-2]) {                TEMP_NEXT.II = Temp.ii + 1;                TEMP_NEXT.JJ = temp.jj-2;          if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;      } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                } if (Temp.ii > 0 && temp.jj > n-1 &&!vis[temp.ii-1][temp.jj-2]) {                TEMP_NEXT.II = temp.ii-1;                TEMP_NEXT.JJ = temp.jj-2;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);                } if (Temp.ii > 1 && temp.jj > 0 &&!vis[temp.ii-2][temp.jj-1]) {                TEMP_NEXT.II = temp.ii-2;                TEMP_NEXT.JJ = temp.jj-1;                if (Temp_next.ii = = Ep.ii && TEMP_NEXT.JJ = = ep.jj) {return step;                } VIS[TEMP_NEXT.II][TEMP_NEXT.JJ] = true;            Q.push (Temp_next);    }}} return-1;}        int main () {int case_num;    scanf ("%d", &case_num);        while (case_num--) {scanf ("%d", &n);        scanf ("%d%d%d%d", &sp.ii, &AMP;SP.JJ, &AMP;EP.II, &AMP;EP.JJ);    printf ("%d\n", BFS ());                  } return 0;}

Sicily 1781. Knight

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.