== Knight Moves == {{{ Time limit: 1 Seconds Memory limit: 32768K Total Submit: 876 Accepted Submit: 487 }}} 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 Specification 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 Specification 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. }}} {{{ #!cplusplus /* zju 1091 经典:跳马问题 ymc 2008/9/10 题目大意: 一只马在8×8的棋盘上从一个位置跳到另外一个位置所需要的最少步数。 分析与解题思路: 从起点开始BFS搜索。还可以简历一个图,任意两个点之间的最短路径。 */ #include using namespace std; int sx,sy,ex,ey; int dist[8][8]; int q[200]; int step[8][2]={{1,2},{1,-2},{-1,2},{-1,-2}, {2,1},{2,-1},{-2,1},{-2,-1},}; int BFS() { memset(dist,-1,sizeof(dist)); dist[sx][sy]=0;//到起点的距离为0 int start=0; int end=0; q[end++]=sx;//起点进入队列 q[end++]=sy; int x1,y1,x2,y2; while(start7||y2<0||y2>7||dist[x2][y2]>=0) continue; dist[x2][y2]=dist[x1][y1]+1; q[end++]=x2; q[end++]=y2; } } return -1;//无解,此题必然有解 } int main() { char s[3],e[3]; int ans; while(scanf("%2s %2s",s,e)!=EOF) { sx=s[0]-'a'; sy=s[1]-'1'; ex=e[0]-'a'; ey=e[1]-'1'; ans=BFS(); printf("To get from %s to %s takes %d knight moves.\n",s,e,ans); } } }}} {{{ #!cplusplus /* zju 1091 经典:跳马问题 ymc 2008/8/30 题目大意: 一只马在8×8的棋盘上从一个位置跳到另外一个位置所需要的最少步数。 分析与解题思路: 从起点开始BFS搜索。还可以简历一个图,任意两个点之间的最短路径。 */ #include #include using namespace std; int sx,sy,ex,ey; int dist[8][8]; int step[8][2]={{-1,2},{-1,-2},{1,2},{1,-2},\ {2,1},{2,-1},{-2,1},{-2,-1}}; int BFS() { memset(dist,-1,sizeof(dist)); queue q; int x1,y1,x2,y2; dist[sx][sy]=0; q.push(sx); q.push(sy); while(!q.empty()) { x1=q.front();q.pop(); y1=q.front();q.pop(); if(x1==ex&&y1==ey) return dist[x1][y1]; for(int i=0;i<8;i++) { x2=x1+step[i][0]; y2=y1+step[i][1]; if(x2<0||x2>7||y2<0||y2>7||dist[x2][y2]>0) continue; dist[x2][y2]=dist[x1][y1]+1; q.push(x2); q.push(y2); } } return -1; } int main() { char a[3],b[3]; int ans; while(scanf("%2s %2s",a,b)!=EOF) { sx=a[0]-'a'; sy=a[1]-'1'; ex=b[0]-'a'; ey=b[1]-'1'; ans=BFS(); printf("To get from %s to %s takes %d knight moves.\n",a,b,ans); } } }}}