1. 程式人生 > >POJ 2243 Knight Moves【A*演算法入門演練】

POJ 2243 Knight Moves【A*演算法入門演練】

Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10412 Accepted: 5886

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 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


很裸的 BFS 題目,為了解決HDU上多組資料的八數碼問題今天初步學了下 A* 演算法,拿來先練手。

code2: A*演算法

介紹A*演算法的文章中對於這題的 H 有點問題,但是能夠 AC 不過過不了大一點的棋盤,因為棋盤稍大的時候如果按照哈曼頓距離求出的未必是最優解

考慮到 G 是由歐幾里得距離求的,這裡的 H也改成歐幾里得距離

對於演算法的理解還是不夠深刻,多做題了再總結吧Orz

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;

int vis[10][10];
int dir[8][2] = {1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1, -1,2};
char start[3], end[3];
int sx,sy,ex,ey;

struct Node{
    int x,y;
    int step;
    int f,g,h;// f = g + h;
    //G 起點到當前點的費用
    //H 當前點到終點的估計費用【此處是歐幾里得距離】

    Node(){}
    Node(int _x, int _y, int _step, int _g, int _h) {
        x = _x; y = _y; step = _step;

        f = _g + _h;
        g = _g; h = _h;
    }
//構造優先佇列
    bool operator <(const Node &a) const {
        return a.f < f;
    }
};

bool inMap(int x, int y)
{
    if(x <= 0 || x > 8 || y <= 0 || y > 8) return false;
    return true;
}

//從(x,y) 到終點的估計費用 【歐幾里得距離】
int getH(int x, int y)
{
    double x1 = x, x2 = ex, y1 = y, y2 = ey; //寫的有點醜。。。
    double dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))*10;
    return (int)dist;
}
void aStart()
{
    priority_queue<Node> q;
    while(!q.empty()) q.pop();

    Node now ,next;
    now = Node(sx,sy,0, 0, getH(sx,sy));

    memset(vis, 0, sizeof(vis));
    vis[sx][sy] = 1;
    q.push(now);

    while(!q.empty())
    {
        now = q.top(); q.pop();

        for(int i = 0; i < 8; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];

            if(!vis[next.x][next.y] && inMap(next.x, next.y))
            {
                vis[next.x][next.y] = 1;
                next.step = now.step + 1;
                next.g = now.g + 23;
                next.h = getH(next.x, next.y);
                next.f = next.g + next.h;
                q.push(next);

                if(next.x == ex && next.y == ey)
                {
                    printf("To get from %c%c to %c%c takes %d knight moves.\n", start[0], start[1], end[0], end[1], next.step);
                    return;
                }
            }
        }
    }
    return;
}
int main()
{
    while(scanf("%s%s", start, end) != EOF)
    {
        sx = start[0] - 'a' + 1;
        sy = start[1] - '0';

        ex = end[0] - 'a' + 1;
        ey = end[1] - '0';

        if(sx == ex && sy == ey)
        {
            printf("To get from %c%c to %c%c takes 0 knight moves.\n", start[0], start[1], end[0], end[1]);
            continue;
        }

        aStart();
    }
    return 0;
}


code1:裸BFS 程式碼

//裸 BFS 解法
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

int vis[10][10];
int dir[8][2] = {1,2, 2,1, 2,-1, 1,-2, -1,-2, -2,-1, -2,1, -1,2};
char start[3], end[3];
int sx,sy,ex,ey;


struct Node{
    int x,y;
    int step;
};

bool inMap(int x, int y)
{
    if(x <= 0 || x > 8 || y <= 0 || y > 8) return false;
    return true;
}
void bfs()
{
    queue<Node> q;
    while(!q.empty()) q.pop();

    Node now, next;
    now.x = sx, now.y = sy; now.step = 0;

    memset(vis, 0, sizeof(vis));
    vis[sx][sy] = 1;
    q.push(now);

    while(!q.empty())
    {
        now = q.front(); q.pop();

        for(int i = 0; i < 8; i++)
        {
            next.x = now.x + dir[i][0];
            next.y = now.y + dir[i][1];

            if(!vis[next.x][next.y] && inMap(next.x, next.y))
            {
                vis[next.x][next.y] = 1;
                next.step = now.step + 1;

                q.push(next);
                if(next.x == ex && next.y == ey)
                {
                    printf("To get from %c%c to %c%c takes %d knight moves.\n", start[0], start[1], end[0], end[1], next.step);
                    return;
                }
            }
        }
    }

    return;
}
int main()
{
    while(scanf("%s%s", start, end) != EOF)
    {
        sx = start[0] - 'a' + 1;
        sy = start[1] - '0';

        ex = end[0] - 'a' + 1;
        ey = end[1] - '0';

        if(sx == ex && sy == ey)
        {

            printf("To get from %c%c to %c%c takes 0 knight moves.\n", start[0], start[1], end[0], end[1]);
        }
        else bfs();

    }
    return 0;
}