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

Knight Moves

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15633   Accepted: 8706

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

Ulm Local 1996

 

 

題目意思就是:給你一個國際象棋的棋盤,一個馬棋子從a點走到b點的最小距離是多少。

 

這題可以用很普通的用bfs來解決。但是bfs效率低。

我又去學習了一種新的搜索方式,A*搜索。

 

就是在搜索的過程中賦予每個點搜索價值,每次先對價值高的點進行討論,這樣可以節省大量時間。

這裏附上一個學習A*算法的博客:http://www.cppblog.com/mythit/archive/2009/04/19/80492.aspx

我的代碼也借學習於此博客。

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>

using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 10;
const int INF = 0x3f3f3f3f;
int X[9] = {-2,-2,-1,-1,1,1,2,2};
int Y[9] = {1,-1,2,-2,-2,2,1,-1};
int ex,ey;
char str[MAXN];
int vis[MAXN][MAXN];
struct Node
{
    int x,y;
    int step;
    int g,h,f;
    /*
    這裏:

    * G = 從起點A,沿着產生的路徑,移動到網格上指定方格的移動耗費。

    * H = 從網格上那個方格移動到終點B的預估移動耗費
    */
    bool operator < (const Node &k) const
    {
        return f>k.f;
    }
} temp;
priority_queue<Node> q;
int manhadun(Node temp)///曼哈頓距離
{
    return (abs(ex - temp.x) + abs(ey - temp.y)) *10;
}
void init()
{
    M(vis,0);
    temp.g = temp.h = temp.f = temp.step  = 0;
    while(!q.empty()) q.pop();
}
int Astar (Node temp)
{
    q.push(temp);
    while(!q.empty())
    {
        Node top = q.top();
        q.pop();
        vis[top.x][top.y] = true;
        if(top.x == ex && top.y == ey)
        {
            return top.step;
        }
        for(int i=0; i<8; i++)
        {
            Node ss;
            ss.x = top.x + X[i];
            ss.y = top.y + Y[i];
            if(ss.x<8&&ss.x>=0&&ss.y<8&&ss.y>=0&&vis[ss.x][ss.y] == 0)
            {
//                printf("x==%d y==%d\n",ss.x,ss.y);
                ss.g = top.g + 23; /// 因爲馬每次走的是 2^2 + 1 = 5。 根號5*10 約等於23;
                ss.h = manhadun(ss);
                ss.f = ss.g + ss.h;
                ss.step  = top.step + 1;
                q.push(ss);
            }

        }
    }
}
int main()
{
    while(gets(str))
    {
        init();
        temp.x = str[0] - 'a';
        temp.y = str[1] - '1';
        ex = str[3] - 'a';
        ey = str[4] - '1';
        temp.h = manhadun(temp);
        temp.f = temp.g + temp.h;
        int ans = Astar(temp);
        printf("To get from %c%c to %c%c takes %d knight moves.\n",temp.x+'a',temp.y+'1',ex+'a',ey+'1',ans);

    }
    return 0;
}
//0 1 0 1 0
//1 0 0 0 1
//0 0 1 0 0
//1 0 0 0 1
//0 1 0 1 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章