#include<stdio.h> #include<string.h> #include<queue> using namespace std; struct K { int x,y,step; }start,end; char s[3],e[3]; int f[][2]={{-1,-2},{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2}},v[8][8]; int bfs() { memset(v,0,sizeof(v)); struct K head,tail; queue<struct K> q; q.push(start); start.step=0; v[start.x][start.y]=1; while(!q.empty()) { head=q.front(); q.pop(); for(int i=0;i<8;i++) { tail.x=head.x+f[i][0]; tail.y=head.y+f[i][1]; tail.step=head.step+1; if(!v[tail.x][tail.y]&&tail.x>=0&&tail.x<8&&tail.y>=0&&tail.y<8) { if(tail.x==end.x&&tail.y==end.y) return tail.step; v[tail.x][tail.y]=1; q.push(tail); } } } } int main() { while(scanf("%s%s",s,e)!=EOF) { start.x=s[1]-'0'-1; start.y=s[0]-'a'; end.x=e[1]-'0'-1; end.y=e[0]-'a'; if(!strcmp(s,e)) printf("To get from %s to %s takes 0 knight moves.\n",s,e); else printf("To get from %s to %s takes %d knight moves.\n",s,e,bfs()); } return 0; } /************************************************************** Problem: 2124 User: admin Language: C++ Result: Compile Error ****************************************************************/