您的位置:首页 > 其它

Knight Moves(HDU 1372)(BFS)

2016-04-22 23:33 375 查看
A - Knight Moves

Time Limit:1000MS Memory Limit:32768KB

题目:

HDU 1372

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.


题目大意:

国际象棋中马可以向 * 走一步,再向 * 走两步,然后就有了dx[8],dy[8],的走法了,之后用bfs求最短路(bfs树,最短路树),然后需要从e2走到e4类似这样的格式。

注意因为是char和int混搭又是多组数据,所以是要注意吸收掉\n(回车)。

熟练运用pair,queue!!这个几乎是模板题了。

bfs伪代码:

通常用队列(先进先出,FIFO)实现

初始化队列Q.
Q={起点s}; 标记s为己访问;
while (Q非空) {
取Q队首元素u; u出队;
if (u == 目标状态) {…}
所有与u相邻且未被访问的点进入队列;
标记u为已访问;
}


代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
using namespace std;
#define P pair<int,int>
#define inf 0x3f3f3f3f
const int maxn = 64;
int map[8][8];
int dx[8] = { -1, -1, 1, 1, -2, -2, 2, 2 }, dy[8] = { 2, -2, 2, -2, 1, -1, 1, -1 };
int sx, sy, gx, gy;
int res;
bool check(int x,int y)
{
return x >= 0 && x < 8 && y >= 0 && y < 8;
}
int bfs()
{
queue<P> que;
//所有位置初始化为INF
memset(map, inf, sizeof(map));
que.push(P(sx, sy));
map[sx][sy] = 0;

//不断循环直到队列长度为0
while (!que.empty())
{
//从队列的最前端取出元素
P p = que.front(); que.pop();
//如果取出的状态已经是终点,则结束搜索
if (p.first == gx&&p.second == gy)break;

for (int i = 0; i < 8; i++){
int nx = p.first + dx[i], ny = p.second + dy[i];
//判断是否出界以及是否访问过(d[nx][ny]!=inf即为已经访问过)
if (check(nx, ny) && map[nx][ny] == inf){
//符合条件的话,则加入队列,并且该位置的距离确定为到p的距离+1
que.push(P(nx, ny));
map[nx][ny] = map[p.first][p.second] + 1;
}
}
}
return map[gx][gy];
}
//下面这个是打印路径,从终点回溯步数减一的
/*void print_ans(int ans)
{
vector<P>path;
P p = P(gx, gy);
path.push_back(p);
int nx = gx, ny = gy;
while (map[nx][ny] != 0){
for (int i = 0; i < 8; i++){
nx = p.first + dx[i];
ny = p.second + dy[i];
if (check(nx, ny) && map[nx][ny] == ans-1){
p = P(nx, ny);
path.push_back(p);
ans--;
}
}
}
for (int i = path.size()-1; i >=0; i--)
printf("%c%d\n", path[i].first+'a', path[i].second+1);
}*/
int main()
{
char a, c;
int b, d;
while (~scanf("%c%d %c%d", &a, &b, &c, &d))
{
getchar();
sx =a-'a';
sy = b - 1;
gx = c - 'a';
gy = d - 1;
res = bfs();
//print_ans(res);
printf("To get from %c%d to %c%d takes %d knight moves.\n", a,b,c,d,res);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: