uva439 - Knight Moves 国际象棋的跳马
来源:互联网 发布:知乎 亚马逊退款订单 编辑:程序博客网 时间:2024/06/05 05:34
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。。。
太笨,我是看题解做的。。。
#include<iostream>#include<cstring>#include<queue>#define MAX 10#define WHITE 0#define BLACK 1using namespace std;typedef struct{int x, y, count;}Point;Point star, end, cur, next;int color[MAX][MAX];int ck[8][2] = {-1,-2, 1,-2, 2,1, 2,-1, 1,2, -1,2, -2,1, -2,-1};int bfs(void){memset(color, WHITE, sizeof (color));color[star.x][star.y] = BLACK;star.count = 0;queue<Point> q;q.push(star);while (!q.empty()){cur = q.front();q.pop();if (cur.x == end.x && cur.y == end.y)return cur.count;for (int i = 0; i < 8; i++){next.x = cur.x + ck[i][0];next.y = cur.y + ck[i][1];if (color[next.x][next.y] == BLACK)continue;if (next.x < 1 || next.x > 8 || next.y < 1 || next.y > 8)continue;color[next.x][next.y] = BLACK;next.count = cur.count + 1;if (next.x == end.x && next.y == end.y)return next.count;q.push(next);}}return -1;}int main(){char st, en;int s, e, step;while (cin >> st >> s >> en >> e){star.x = st - 'a' + 1;star.y = s;end.x = en - 'a' + 1;end.y = e;if (star.x == end.x && star.y == end.y)step = 0;elsestep = bfs();cout << "To get from "<< st << s << " to " << en << e << " takes " << step << " knight moves." << endl;}return 0;}
- uva439 - Knight Moves 国际象棋的跳马
- UVA439- Knight Moves
- Knight Moves UVA439
- uva439 - Knight Moves
- UVA439 - Knight Moves
- UVa439 Knight Moves
- uva439 Knight Moves
- uva439 Knight Moves
- uva439 -- Knight Moves
- Knight Moves(跳马)
- Uva439——Knight Moves
- UVa439 习题 6-4 骑士的移动(Knight Moves)
- poj_2243/uva439 Knight Moves(bfs經典)
- UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves
- UVa439---Knight Moves(BFS应用)
- 【UVA439】 Knight Moves BFS (3/1000)
- POJ 2243-Knight Moves(DFS-跳马)
- POJ 1915-Knight Moves(BFS-跳马)
- 求二叉树中两结点的最小公共祖先
- open与fopen对比
- 0421周赛 HDU 1498 二分匹配
- 关于instanceof的使用
- 简单工厂模式(SimpleFactory)
- uva439 - Knight Moves 国际象棋的跳马
- 我对ORACLE 弹性域的理解
- Keil中 Program Size: Code RO-data RW-data ZI-data 所代表的意思
- Halcon函数总结(八)
- 剑指offer面试题1 MyString赋值运算符的两种写法
- 第24天的交易(2013-4-23)(-100)(-863.65)(失望的一天)
- struct timeval结构体 以及 gettimeofday()函数
- 内存分配的api的异同
- Android中使用ListView绘制自定义表格