Knight Moves(HDU 1372)(BFS)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值