Knight Moves
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8831 Accepted Submission (s): 5202
Problem Descriptiona friend of you doing in the traveling Knight problem (TKP) where you were to find the short EST 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.
Inputthe input file would 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 CH Essboard.
Outputfor each test case, print one line saying "to get from XX to YY takes N Knight moves."
E2 e4//Start point
A1 B2
B2 C3
A1 H8
A1 H7
H8 A1
B1 C3
F6 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.
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5 using namespacestd;6 intdx[]={1,-1,1,-1,2,-2,2,-2};7 intdy[]={2,-2,-2,2,1,-1,-1,1};8 intvis[9][9];9 struct PointTen { One intx; A inty; - intT; - }st,tem,nex; the intSx,sy,ex,ey,tim; - Chara,b,c,d; - voidBFS () - { +Queue<point>s; -st.x=sx,st.y=Sy; +st.t=0; A S.push (ST); atmemset (Vis,0,sizeof(Vis)); - while(!s.empty ()) - { -tem=S.front (); - S.pop (); - in if(tem.x==ex&&tem.y==ey) - { totim=tem.t; + return; - } the if(vis[tem.x][tem.y]| | tem.x<=0|| tem.y<=0|| Tem.x>8|| Tem.y>8) * Continue; $vis[tem.x][tem.y]=1;Panax Notoginseng for(intI=0;i<8; i++) - { the intnx=tem.x+Dx[i]; + intny=tem.y+Dy[i]; A intnt=tem.t+1; thenex.x=nx,nex.y=ny,nex.t=NT; + S.push (NEX); - } $ } $ } - intMain () - { theFreopen ("In.txt","R", stdin); - while(SCANF ("%c%c%c%c", &a,&b,&c,&d)! =EOF)Wuyi { the GetChar (); -tim=0; Wusy=a-'a'+1, sx=b-'0'; -ey=c-'a'+1, ex=d-'0'; About BFS (); $printf"To get from%c%c to%c%c takes%d Knight moves.\n", A,b,c,d,tim); - } -}
Knight Moves (BFS, walk ' Day ' word)