Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 19
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.
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.
Source
University of Ulm Local Contest 1996
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<string> #include<fstream> using namespace std; bool used[310][310]; struct squ{ char x; char y; int a; int b; long step; }; long bfs(char x1,char y1,char x2,char y2){ queue<squ>que; squ tp,ans,des; squ tn; int e; int der[8][2] = {{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{-2,1},{2,-1},{-2,-1}}; int i; ans.x=x1;ans.a=x1-'a'+1; ans.y=y1;ans.b=y1-'0'; des.x=x2;des.a=x2-'a'+1; des.y=y2;des.b=y2-'0'; ans.step=0; used[ans.a][ans.b]=false; que.push(ans); //printf("dd"); while(1){ tp=que.front(); que.pop(); //printf("dd"); for(e=0;e<8;e++){ tn.a=tp.a+der[e][0]; tn.b=tp.b+der[e][1]; if(tn.a>=1&&tn.a<=8&&tn.b>=1&&tn.b<=8){ if(used[tn.a][tn.b]){ used[tn.a][tn.b]=false; ans.a=tn.a; ans.b=tn.b; ans.step=tp.step+1; que.push(ans); if(ans.a==des.a&&ans.b==des.b) return ans.step; } } } } } int main(){ char s1[3],s2[3]; int sum; //ifstream cin("input.txt"); //freopen("input .txt","r",stdin); while(scanf("%s %s",s1,s2)!=EOF){ //while(gets(s1)!=NULL){ memset(used,true,sizeof(used)); if(!strcmp(s1,s2)) cout<<"To get from "<<s1<<" to "<<s2<<" takes 0 knight moves."<<endl; else{ sum=bfs(s1[0],s1[1],s2[0],s2[1]); cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<sum<<" knight moves."<<endl; } //printf("dd"); } return 0; } |
code
more code
~~~~