HDU1372 Knight Moves (BFS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xMaii/article/details/75210345

Knight Moves

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11703 Accepted Submission(s): 6890

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.

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.

Source
University of Ulm Local Contest 1996

该题是学习BFS的经典题目,BFS的思路大致如下:
将骑士的初始位置入队,出队,向其八个方向扩展状态,且只把其中的合法状态入队,再重复出队、扩展状态……直到到达目标位置,否则,即当队列为空时,无解。

代码如下。

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

struct Direction    //方向偏移量
{
    int x, y;
}dir[8] = { { 1,2 },{ 1,-2 },{ -1,2 },{ -1,-2 },{ 2,1 },{ 2,-1 },{ -2,1 },{ -2,-1 } };

struct PosNode
{
    int x, y;
    int steps;
};

queue<PosNode>que;
char c[6];
PosNode sta, des;       //存储起点和终点的坐标
bool vis[9][9];         //用于记忆化搜索

bool WhetherIn(int x, int y)    //判断坐标(x,y)是否合法
{
    if (x < 1 || x>8 || y < 1 || y>8)
        return false;
    return true;
}

int BFS()
{
    while (!que.empty())
    {
        PosNode cur = que.front(); que.pop();
        if (cur.x == des.x && cur.y == des.y)
            return cur.steps;
        for (int i = 0; i < 8; i++)
        {
            PosNode ext;
            ext.x = cur.x + dir[i].x;
            ext.y = cur.y + dir[i].y;
            ext.steps = cur.steps + 1;
            if (WhetherIn(ext.x, ext.y) && !vis[ext.x][ext.y])
            {
                que.push(ext);
                vis[ext.x][ext.y] = true;
            }
        }
    }
}

void Init()
{
    while (!que.empty())
        que.pop();
    for (int i = 0; i <= 8; i++)
        for (int j = 0; j <= 8; j++)
            vis[i][j] = false;
    sta.x = c[0] - 'a' + 1;
    sta.y = c[1] - '0';
    sta.steps = 0;
    des.x = c[3] - 'a' + 1;
    des.y = c[4] - '0';
    que.push(sta);
    vis[sta.x][sta.y] = true;
}
int main()
{
    while (gets(c))
    {
        Init();
        cout << "To get from " << c[0] << c[1] << " to " << c[3] << c[4] << " takes " << BFS() << " knight moves." << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xMaii/article/details/75210345