Knight Moves
来源:互联网 发布:零境网络有哪些游戏 编辑:程序博客网 时间:2024/05/01 05:45
Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 45 Accepted Submission(s) : 13
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.
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 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.
Source
PKU
BFS
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;queue<int>Q;int x, y, x1, Y, sum;char a[2], b[2];int c[8][8] ,c1[8][8];int d[8][2] = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { 1, -2 }, { -1, 2 }, { 1, 2 }, { 2, -1 }, { 2, 1 } }; // 方向int d1[4][2] = { { -1, 0 }, { 0, -1 }, { 0, 1 }, { 1, 0 } }; // 防马蹄void BFS(){int x2, y2;while (!Q.empty() && !c[x1][Y]){x2 = Q.front(); Q.pop();y2 = Q.front(); Q.pop();for (int i = 0; i < 8; i++){if ((x2 + d[i][0]) >= 0 && (x2 + d[i][0]) <= 7 && (y2 + d[i][1]) >= 0 && (y2 + d[i][1]) <= 7 && !c[x2 + d[i][0]][y2 + d[i][1]]) // 不越界 不从走{//if (!((x2 + d1[i / 2][0]) == x && (y2 + d1[i / 2][1]) == y) &&!((x2 + d1[i / 2][0]) == x1 && (y2 + d1[i / 2][1])== Y)) // 不挡腿//{c[x2 + d[i][0]][y2 + d[i][1]] = 1;c1[x2 + d[i][0]][y2 + d[i][1]] = c1[x2][y2] + 1;Q.push(x2 + d[i][0]); Q.push(y2 + d[i][1]);//}}}}}int main(){while (scanf("%s%s", &a, &b) != EOF){x = y = x1 = Y = 0;x = a[1] - '1';y = a[0] - 'a'; x1 = b[1] - '1';Y = b[0] - 'a'; memset(c, 0, sizeof(c));memset(c1, 0, sizeof(c1));while (!Q.empty())Q.pop();Q.push(x); Q.push(y);c[x][y] = 1;BFS();printf("To get from %c%c to %c%c takes %d knight moves.\n",a[0],a[1],b[0],b[1], c1[x1][Y]);}return 0;}
0 0
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- hjewgkjlkdjfygdjhkjndhjgusdlkfd
- Windows常见蓝屏故障分析
- hibernate一对一外键双向关联
- leetcode 虐我篇之(二) Two Sum
- 进入Ubuntu图形桌面的方法
- Knight Moves
- opencv 移植
- [bash script] 初始化一个 node.js 应用开发 working tree
- 发的房东和地方好的
- fytgdshlkdfhmglgjhvoj
- 线段树离线处理专题
- POJ2536_Gopher II(二分图最大匹配)
- C回调函数与java回调方法
- w6e893 bdn92z10