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.
1 /*
2 zju 1091 经典:跳马问题
3 ymc 2008/9/10
4 题目大意:
5 一只马在8×8的棋盘上从一个位置跳到另外一个位置所需要的最少步数。
6 分析与解题思路:
7 从起点开始BFS搜索。还可以简历一个图,任意两个点之间的最短路径。
8 */
9 #include <iostream>
10 using namespace std;
11 int sx,sy,ex,ey;
12 int dist[8][8];
13 int q[200];
14 int step[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},
15 {2,1},{2,-1},{-2,1},{-2,-1},};
16 int BFS()
17 {
18 memset(dist,-1,sizeof(dist));
19 dist[sx][sy]=0;//到起点的距离为0
20 int start=0;
21 int end=0;
22 q[end++]=sx;//起点进入队列
23 q[end++]=sy;
24 int x1,y1,x2,y2;
25 while(start<end)//队列不空
26 {
27 x1=q[start];start++;//取队首元素
28 y1=q[start++];
29 if(x1==ex&&y1==ey)//达到终点
30 return dist[x1][y1];
31 for(int k=0;k<8;k++)//向8个方向各跳一步,为走过的有效位置进入队列
32 {
33 x2=x1+step[k][0];
34 y2=y1+step[k][1];
35 if(x2<0||x2>7||y2<0||y2>7||dist[x2][y2]>=0)
36 continue;
37 dist[x2][y2]=dist[x1][y1]+1;
38 q[end++]=x2;
39 q[end++]=y2;
40 }
41 }
42 return -1;//无解,此题必然有解
43 }
44 int main()
45 {
46 char s[3],e[3];
47 int ans;
48 while(scanf("%2s %2s",s,e)!=EOF)
49 {
50 sx=s[0]-'a';
51 sy=s[1]-'1';
52 ex=e[0]-'a';
53 ey=e[1]-'1';
54 ans=BFS();
55 printf("To get from %s to %s takes %d knight moves.\n",s,e,ans);
56 }
57 }
1 /*
2 zju 1091 经典:跳马问题
3 ymc 2008/8/30
4 题目大意:
5 一只马在8×8的棋盘上从一个位置跳到另外一个位置所需要的最少步数。
6 分析与解题思路:
7 从起点开始BFS搜索。还可以简历一个图,任意两个点之间的最短路径。
8 */
9 #include <iostream>
10 #include <queue>
11 using namespace std;
12 int sx,sy,ex,ey;
13 int dist[8][8];
14 int step[8][2]={{-1,2},{-1,-2},{1,2},{1,-2},\
15 {2,1},{2,-1},{-2,1},{-2,-1}};
16 int BFS()
17 {
18 memset(dist,-1,sizeof(dist));
19 queue<int> q;
20 int x1,y1,x2,y2;
21 dist[sx][sy]=0;
22 q.push(sx);
23 q.push(sy);
24 while(!q.empty())
25 {
26 x1=q.front();q.pop();
27 y1=q.front();q.pop();
28 if(x1==ex&&y1==ey)
29 return dist[x1][y1];
30 for(int i=0;i<8;i++)
31 {
32 x2=x1+step[i][0];
33 y2=y1+step[i][1];
34 if(x2<0||x2>7||y2<0||y2>7||dist[x2][y2]>0)
35 continue;
36 dist[x2][y2]=dist[x1][y1]+1;
37 q.push(x2);
38 q.push(y2);
39 }
40 }
41 return -1;
42 }
43 int main()
44 {
45 char a[3],b[3];
46 int ans;
47 while(scanf("%2s %2s",a,b)!=EOF)
48 {
49 sx=a[0]-'a';
50 sy=a[1]-'1';
51 ex=b[0]-'a';
52 ey=b[1]-'1';
53 ans=BFS();
54 printf("To get from %s to %s takes %d knight moves.\n",a,b,ans);
55 }
56 }