Test instructions The minimum number of steps required for a knight to move east from one position to another in chess
The basic BFS Application
#include <bits/stdc++.h>using namespace Std;int x[] = {-2,-1,-2,-1, 1, 2, 1, 2};int y[] = {-1,-2, 1, 2,-2, 1 , 2, 1};int d[15][15], SX, SY, EX, Ey;pair<int, Int> q[105], T;int BFS () {int CX, CY, R, c, front = 0, rear = 0; memset (d,-1, sizeof (d)); D[sx][sy] = 0; q[rear++] = Make_pair (SX, SY); while (front < rear) {t = q[front++]; CX = t.first, cy = T.second; if (cx = = Ex && cy = = ey) return d[cx][cy]; for (int i = 0; i < 8; ++i) {r = cx + x[i], C = cy + y[i]; if (R < 0 | | r > 7 | | C < 0 | | C > 7 | | d[r][c] >= 0) continue; D[R][C] = D[cx][cy] + 1; q[rear++] = Make_pair (R, c); }}}int Main () {char a[5], b[5]; while (~SCANF ("%s%s", A, B)) {SX = a[0]-' a ', sy = a[1]-' 1 '; ex = b[0]-' a ', EY = b[1]-' 1 '; printf ("To get from%s to%s takes%d Knight moves.\n", A, B, BFS ()); } return 0;}
A Friend of you doing in the traveling Knight problem (TKP) where you were to find the shortest closed Tour of Knight moves this visits each square of a given set of n squares on a chessboard exactly once. He thinks the most difficult part of the problem is determining the smallest number of knight moves between the given Squares and that, once you has accomplished this, finding the tour would is easy.
Of course you know the It is vice versa. Him to write a program that solves the "difficult" part.
Your job is to write a program, takes, squares a and b as input and then determines the number of Knight moves on a shortest route from a to b.
Input Specification
The input file is contain one or more test cases. Each test case consists the one line containing the squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing The row on the chessboard.
Output Specification
For each test case, the print one line saying "toget from xx to yy takes n Knight moves. ".
Sample Input
E2 E4A1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 f6
Sample Output
To get from E2 to E4 takes 2 Knight moves. To get from A1 to B2 takes 4 knight moves. To get from B2 to C3 takes 2 knight moves. To get from A1 to H8 takes 6 knight moves. To get from A1 to H7 takes 5 knight moves. To get from H8 to A1 takes 6 knight moves. To get from B1 to C3 takes 1 Knight moves. To get from F6 to F6 takes 0 knight moves.
UVa 439 Knight Moves (BFS applications)