Knight Moves
来源:互联网 发布:算法复杂度计算 编辑:程序博客网 时间:2024/05/01 07:10
Knight Moves
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 ofn 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 froma 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.
大意:
8*8棋盘中,给出起点和终点,求出马最少走几步;
要点:
bfs;
代码:
#include <iostream>#include <cstdio>#include <string.h>#include <queue>using namespace std;char map[8][8];int dir[8][2] = { { -2, -1 }, { -1, -2 }, { 1, -2 }, { 2, -1 }, { 2, 1 }, { 1, 2 }, { -1, 2 }, { -2, 1 } };struct node {int x;int y;int dis;node(int a = 0, int b = 0, int c = 0) :x(a), y(b), dis(c){}};queue <node *> que;bool judge(int x, int y){if (x < 0 || y < 0 || x > 7 || y > 7)return false;if (map[x][y] == '*')return false;return true;}int bfs(int x, int y, int d){if (map[x][y] != '#'){map[x][y] = '*';if (!que.empty()){node *fr = que.front();que.pop();for (int i = 0; i < 8; i++){int a = fr->x + dir[i][0];int b = fr->y + dir[i][1];if (judge(a, b)){node *n = new node(a, b, fr->dis + 1);que.push(n);}}node *t = que.front();bfs(t->x, t->y, t->dis);}}else{return d;}}int main(){char str1[3], ch, str2[3];while (scanf ("%s%s", str1, str2) != EOF){memset(map, '0', sizeof(map));int begx, begy, endx, endy;begx = str1[0] - 'a';begy = str1[1] - '0' - 1;endx = str2[0] - 'a';endy = str2[1] - '0' - 1;map[endx][endy] = '#';node *t = new node(begx, begy, 0);que.push(t);int ans = bfs(begx, begy, 0);printf("To get from %s to %s takes %d knight moves.\n", str1, str2, ans);while (!que.empty())que.pop();}return 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
- 用js -- interval 实现当前时间
- C语言 位操作
- jquery+ajax(asp.net)
- linux c编程学习——学习步骤
- oracle学习笔记——sql符号解释
- Knight Moves
- javascript对象创建
- 黑马程序员——C语言内存管理
- oracle学习笔记——事物槽
- 【特征匹配】SURF原理与源码解析(二)
- 拉格朗日乘子法和KKT条件
- oracle学习笔记——存储参数
- 初识MVC
- oracle学习笔记——date数据及相关函数