这周作业ProblemB。
传送门:http://acm.hit.edu.cn/hojx/showproblem/1440/
1440 - Knight Moves
Time limit : 1 s Memory limit : 32 mb
Submitted : 1311 Accepted : 472
Submit
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后一个小小的进阶,所以在很多OJ上都有收录。
同一份代码,在HDU那里是AC, POJ上是RE,HOJ上是WA……总的来说还是比较奇怪的,可能各个OJ都有各自的特点吧。HOJ和POJ的数据都较强一些。后来我看了看代码,的确有许多需要改进的小地方.后来都改了改,然后都过了。
这个题的BFS就是要读懂样例,不过如果之前玩过Chess的同学都知道,马走日。而且棋子是占格子而不是占交点。所以有八个方向,搞清楚后大概就没什么问题了。
AC代码如下:
#include
#include
#include
using namespace std;
#define maxb 9
#define sq 2
int rch[maxb][maxb];
struct Node{
int x, y;
int step;
};
int sx, sy;
int ex, ey;
queue knight;
int dx[9] = {0, 1, 1, -1, -1, 2, 2, -2, -2};
int dy[9] = {0, 2, -2, 2, -2, 1, -1, 1, -1};
int BFS(void)
{
while(!knight.empty()) knight.pop();
memset(rch, 0, sizeof(rch));
Node beg;
beg.x = sx, beg.y = sy, beg.step = 0;
knight.push(beg);
rch[beg.x][beg.y] = 1;
while(!knight.empty())
{
Node cur = knight.front();
knight.pop();
if(cur.x == ex && cur.y == ey)
return cur.step;
for(int i = 1; i < 9; i++)
{
Node next;
next.x = cur.x + dx[i];
next.y = cur.y + dy[i];
next.step = cur.step;
if(rch[next.x][next.y] || next.x < 1 || next.x > 8 || next.y < 1 || next.y > 8) continue;
if(next.x == ex && next.y == ey)
return next.step+1;
next.step += 1;
rch[next.x][next.y] = 1;
knight.push(next);
}
}
return 0;
}
int main()
{
char s1[sq], s2[sq];
while(scanf("%s %s", s1, s2) != EOF)
{
sx = s1[0] - 'a' + 1;
sy = s1[1] - '0';
ex = s2[0] - 'a' + 1;
ey = s2[1] - '0';
// printf("%d %d\n%d %d\n", sx, sy, ex, ey);
printf("To get from %s to %s takes %d knight moves.\n", s1, s2, BFS());
}
return 0;
}