<span style="color:#330099;">/* C - 广搜 基础 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status 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 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. By Grant Yuan 2014.7.12 */ #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<queue> using namespace std; char a[9]={'0','a','b','c','d','e','f','g','h'}; char b[9]={'0','1','2','3','4','5','6','7','8'}; bool flag[9][9]; int next[8][2]={1,2,2,1,-1,2,2,-1,-2,-1,-1,-2,-2,1,1,-2}; char s1[5],s2[2]; char s[10]; int x1,x2,f1,f2; int best=100; typedef struct{ int x; int y; int sum; }node; node q[100000]; bool can(int x,int y) { if(x<=8&&x>0&&y<=8&&y>0&&flag[x][y]==0) return 1; return 0; } int ree; int top,base; void slove() {int xx,yy,count,m,n; node q1; while(top>=base){ if(q[base].x==x2&&q[base].y==f2){ ree=q[base].sum; break; } else{ m=q[base].x; n=q[base].y; count=q[base].sum; for(int i=0;i<8;i++) { xx=m+next[i][0]; yy=n+next[i][1]; if(can(xx,yy)){ // cout<<xx<<" "<<yy<<endl; flag[xx][yy]=1; q[++top].x=xx; q[top].y=yy; q[top].sum=count+1; } } }base++; } } int main() {node q1; while(scanf("%s%s",&s1,&s2)!=EOF){ //puts(s1); //puts(s2); // for(int i=0;i<=1;i++) // s2[i]=s1[i+2]; /*for(int i=0;i<=1;i++) s2[i]=s[i+2];*/ // puts(s1); // puts(s2); s1[2]=' '; // puts(s1); top=-1;base=0; memset(flag,0,sizeof(flag)); for(int i=1;i<=8;i++) { if(s1[0]==a[i]) x1=i; if(s1[1]==b[i]) f1=i; if(s2[0]==a[i]) x2=i; if(s2[1]==b[i]) f2=i; } //cout<<x1<<" "<<f1<<" "<<x2<<" "<<f2<<endl; flag[x1][f1]=1; q[++top].x=x1; q[top].y=f1; q[top].sum=0; slove(); printf("To get from %s to %s takes %d knight moves. ",s1,s2,ree); } return 0; } </span>