HDU 1372 Knight Moves 广度优先搜索 bfs
2015-04-30 10:57
435 查看
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7987 Accepted Submission(s): 4706
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.
Source
University of Ulm Local Contest 1996
Recommend
Eddy
这一题并没有什么特别技巧,只不过有两点需要注意一下 1.骑士走的方向是8个方向。2 是s[y]=s1[1]-'1',减去的应该是1,我开始写的是减.0,调试了老半天.
#include<iostream> #include<cstdio> #include<queue> #include<cstring> using namespace std; int ex, ey, sx, sy; int vis[20][20]; int step[20][20]; struct point { int x; int y; }; queue<point>q; int dirx[10] = { -2, -1, 1, 2, -2, -1, 1, 2 }; int diry[10] = { -1, -2, -2, -1, 1, 2, 2, 1 }; int bfs() { while (!q.empty())//为什么要加这一句呢?因为q是全局变量,所以每次用都要置空队列 q.pop(); point temp, head, tt; temp.x = sx; temp.y = sy; vis[sx][sy] = 1; q.push(temp); while (!q.empty()) { head = q.front(); q.pop(); for (int i = 0; i<8; i++) { tt.x = head.x + dirx[i]; tt.y = head.y + diry[i]; if (tt.x<1 || tt.y<1 || tt.x>8 || tt.y>8 || vis[tt.x][tt.y]) continue; vis[tt.x][tt.y] = 1;//标记为访问过 q.push(tt); step[tt.x][tt.y] = step[head.x][head.y] + 1;//记录步数 if (ex == tt.x&&ey == tt.y) return step[ex][ey]; } } } int main() { char s1[10], s2[10]; while (cin >> s1 >> s2) { sx = s1[0] - 'a' + 1; sy = s1[1] - '1' + 1; ex = s2[0] - 'a' + 1; ey = s2[1] - '1' + 1;// memset(vis, 0, sizeof(vis)); memset(step, 0, sizeof(step)); if (sx == ex&&sy == ey)//若起点坐标为之与终点坐标相同则输出0步 printf("To get from %s to %s takes 0 knight moves.\n", s1, s2); else printf("To get from %s to %s takes %d knight moves.\n", s1, s2, bfs()); } return 0; }
相关文章推荐
- HDU 1372 广度优先搜索(BFS) Knight Moves
- hdu 1372 Knight Moves(bfs)
- HDU - 1372 Knight Moves ( 简单BFS + floodfill )
- (step4.2.1) hdu 1372(Knight Moves——BFS)
- HDU 1372Knight Moves(bfs)
- hdu 1372 Knight Moves (简单bfs,囧!)
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- HDU 1372 Knight Moves 【经典BFS】
- hdu1372 Knight Moves BFS 搜索
- hdu 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(BFS)
- HDU ACM 1372 Knight Moves (BFS)
- hdu 1372 Knight Moves (水bfs)
- Knight Moves(HDU 1372)(BFS)
- HDU 1372 Knight Moves(BFS)
- hdu 1372 Knight Moves bfs搜索 解题报告
- hdu 1372 ( Knight Moves ) BFS
- hdu1372 Knight Moves (BFS)
- HDU 1372 Knight Moves ( BFS & DBFS )
- HDU 1372 Knight Moves (BFS)