【HDOJ-1372】Knight Moves

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.
 
思路:
先把字母转换成数字储存 再BFS
 
参考代码:
【HDOJ-1372】Knight Moves
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 using namespace std;
 5 int movex[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
 6 int mark[10][10];
 7 int step[10][10];
 8 int bfs(int a,int b,int c,int d);
 9 int main()
10 {
11     int a0,b0,an,bn;
12     char temp1,temp2;
13     while(scanf("%c%d %c%d",&temp1,&b0,&temp2,&bn)!=EOF)
14     {
15         getchar();
16         memset(mark,0,sizeof(mark));
17         memset(step,0,sizeof(step));
18         a0=temp1-a+1;
19         an=temp2-a+1;
20         if(temp1==temp2&&b0==bn)
21         {
22             printf("To get from %c%d to %c%d takes 0 knight moves.\n",temp1,b0,temp2,bn);
23             continue;
24         }
25         int ans=bfs(a0,b0,an,bn);
26         printf("To get from %c%d to %c%d takes %d knight moves.\n",temp1,b0,temp2,bn,ans);
27     }
28     return 0;
29 }
30 
31 int bfs(int a,int b,int c,int d)
32 {
33     queue<int> qx;
34     queue<int> qy;
35     qx.push(a);
36     qy.push(b);
37     mark[a][b]=1;
38     while(!qx.empty()&&!qy.empty())
39     {
40         int i;
41         for(i=0;i<8;i++)
42         {
43             int nx=qx.front()+movex[i][0];
44             int ny=qy.front()+movex[i][1];
45             if(nx>=1&&nx<=8&&ny>=1&&ny<=8&&!mark[nx][ny])
46             {
47                 mark[nx][ny]=1;
48                 step[nx][ny]=step[qx.front()][qy.front()]+1;
49                 qx.push(nx);
50                 qy.push(ny);
51                 if(nx==c&&ny==d)
52                     return step[nx][ny];
53             }
54         }
55         qx.pop();
56         qy.pop();
57     }
58 }
【HDOJ-1372】Knight Moves

 

【HDOJ-1372】Knight Moves

上一篇:iOS7 NavigationController 手势问题


下一篇:【HDOJ-1195】Open the Lock