Knight Moves
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 7775
Accepted: 4531
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.
Source
利用BFS可以解决这个问题。
1: //#define DEBUG
2: #include <iostream>
3: #include <cstdio>
4: #include <queue>
5: #include <cstring>
6:
7: using namespace std;
8:
9: struct chess
10: {
11: int x;
12: int y;
13: int step;
14: };
15: int dx[] = {1,1,-1,-1,2,2,-2,-2};
16: int dy[] = {2,-2,2,-2,1,-1,1,-1};
17: bool visit[9][9];
18: char first[3];
19: char last[3];
20: int bfs();
21: int main()
22: {
23: int result;
24: #ifdef DEBUG
25: freopen("test.txt", "r", stdin);
26: freopen("results.txt","w",stdout);
27: #endif
28: while(scanf("%s%s",first,last) != EOF)
29: {
30: result = bfs();
31: printf("To get from %s to %s takes %d knight moves.\n",first,last,result);
32: }
33: return 0;
34: }
35: int bfs()
36: {
37: queue<chess> q;
38: int x = first[0] - 'a' + 1;
39: int y = first[1] - '0';
40: int x1 = last[0] - 'a' +1;
41: int y1 = last[1] - '0';
42: chess s;
43: s.x = x;
44: s.y = y;
45: s.step = 0;
46: q.push(s);
47: memset(visit,false,sizeof(visit));
48: visit[x][y] = true;
49: while(!q.empty())
50: {
51: chess previous = q.front();
52: q.pop();
53: if(previous.x == x1 && previous.y == y1)
54: return previous.step;
55: for(int i=0;i<8;++i)
56: {
57: chess temp;
58: temp.x = previous.x + dx[i];
59: temp.y = previous.y + dy[i];
60: if(temp.x<1||temp.x>8||temp.y<1||temp.y>8)
61: continue;
62: if(visit[temp.x][temp.y] == false)
63: {
64: visit[temp.x][temp.y] = true;
65: temp.step = previous.step + 1;
66: q.push(temp);
67: }
68: }
69: }
70: return 0;
71: }
不知道为什么,bfs()最后的return 0;不能删去,否则就是WA。具体原因我还不能弄清楚,同学说可能是编译器的bug。
通过这个题目还学习到一种以后应该会经常用到的debug技巧。就是利用宏和freopen。具体方法如源代码里面所示。这样可以方便地用文件进行输入输出。