Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12204 Accepted Submission(s):
7158
Problem Description
A friend of you is doing research on the Traveling
Knight Problem (TKP) where you are to find the shortest closed tour of knight
moves that visits each square of a given set of n squares on a chessboard
exactly once. He thinks that the most difficult part of the problem is
determining the smallest number of knight moves between two given squares and
that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input file will contain one or more test cases.
Each test case consists of one line containing two 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
For each test case, print one line saying "To get from
xx to yy takes n knight moves.".
Sample Input
e2 e4
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.
AC代码(BFS+QUEUE)
1 #include<map> 2 #include<fstream> 3 #include<iostream> 4 #include<queue> 5 using namespace std; 6 const int MAX=10; 7 struct data 8 { 9 int x,y,time; 10 }; 11 int vis[MAX][MAX]; 12 int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}}; 13 int x1,x2,x3,x4; 14 int BFS() 15 { 16 if(x1==x3&&x2==x4) 17 return 0; 18 data cur={x1,x2,0},next; 19 queue<data> que; 20 que.push(cur); 21 vis[x1][x2]=1; 22 while(!que.empty()) 23 { 24 cur=que.front(); que.pop(); 25 if(cur.x==x3&&cur.y==x4) 26 return cur.time; 27 for(int i=0;i<8;i++) 28 { 29 next.x=cur.x+dir[i][0]; 30 next.y=cur.y+dir[i][1]; 31 if(next.x>=1&&next.x<=8&&next.y>=1&&next.y<=8&&!vis[next.x][next.y]) 32 { 33 next.time=cur.time+1; 34 vis[next.x][next.y]=1; 35 que.push(next); 36 } 37 } 38 } 39 } 40 int main() 41 { 42 map<char,int> m; 43 m['a']=1;m['b']=2;m['c']=3;m['d']=4; 44 m['e']=5;m['f']=6;m['g']=7;m['h']=8; 45 //ifstream in("data.txt"); 46 int b,d;char a,c; 47 while(cin>>a>>b>>c>>d) 48 { 49 memset(vis,0,sizeof(vis)); 50 x1=b;x2=m[a];//x2=a-96; 51 x3=d;x4=m[c];//x4=c-96; 52 int res=BFS(); 53 cout<<"To get from "<<a<<b<<" to "<<c<<d<<" takes "<<res<<" knight moves."<<endl; 54 } 55 return 0; 56 }