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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章