Knight Moves
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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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
算法分析:直接从起点使用一次BFS并记录到终点时所走过的路径长度即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <iostream> #include <fstream> #include <cstring> #include <queue> using namespace std; const int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1}; const int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int start_x, start_y, end_x, end_y; int ans[36][36]; bool visited[36][36]; struct point { int x; int y; int steps; point (int xx, int yy, int ss) : x (xx), y (yy), steps (ss) {} }; queue <point> q; bool okjump (int x, int y) { if (x >= 0 && x <= 7 && y >= 0 && y <= 7 && !visited[x][y]) return true; return false; } int BFS (int x, int y, int steps) { while (!q.empty ()) //important!!!!!1 q.pop (); memset (visited, false, sizeof (visited)); q.push (point (x, y, steps)); visited[x][y] = true; while (!q.empty ()) { point temp = q.front (); if (temp.x == end_x && temp.y == end_y) return temp.steps; int i; for (i = 0; i < 8; i++) if (okjump (temp.x + dx[i], temp.y + dy[i])) { q.push (point (temp.x + dx[i], temp.y + dy[i], temp.steps + 1)); visited[temp.x+dx[i]][temp.y+dy[i]] = true; } q.pop (); } return 0; } int main() { //ifstream cin ("aaa.txt"); char start_ch[6]; char end_ch[6]; while (cin >> start_ch >> end_ch) { start_x = start_ch[1] - '0'; start_y = start_ch[0] - 'a'; end_x = end_ch[1] - '0'; end_y = end_ch[0] - 'a'; start_x--; end_x--; int sum = BFS (start_x, start_y, 0); cout << "To get from " << start_ch << " to " << end_ch << " takes " << sum << " knight moves." << endl; } return 0; } |
- « 上一篇:zoj1082
- zoj1092:下一篇 »