POJ2243 Knight Moves —— A*算法

来源:互联网 发布:js点击弹出再点击隐藏 编辑:程序博客网 时间:2024/06/06 03:53

题目链接:http://poj.org/problem?id=2243


Knight Moves
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14500 Accepted: 8108

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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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




题解:

这题直接用BFS就可以过了。但是想借助这题学一下A*算法(但以下写法好像不是完整的或者说正确的写法,先放一放,以后遇到再深入)。



代码如下;

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <cmath>using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 8+10;struct node{    int x, y, step;    int g, h, f;    bool operator<(const node&a)const{        return f>a.f;    }};bool vis[MAXN][MAXN];int dir[8][2] = { {1,2}, {1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1} };int gdis(int x_step, int y_step)    //直线距离{    return (int)sqrt(x_step*x_step+y_step*y_step)*10+1;}int hdis(node a, node b)    //曼哈顿距离{    return ( abs(a.x-b.x) + abs(a.y-b.y) )*10;}priority_queue<node>que;int Astar(node st, node en){    memset(vis,false,sizeof(vis));    st.step = st.g = st.h = st.f = 0;    while(!que.empty()) que.pop();    que.push(st);    vis[st.x][st.y] = true;    while(!que.empty())    {        node now = que.top();        que.pop();        if(now.x==en.x && now.y==en.y)            return now.step;        for(int i = 0; i<8; i++)        {            node tmp;            tmp.x = now.x + dir[i][0];            tmp.y = now.y + dir[i][1];            if(tmp.x>=1 && tmp.x<=8 && tmp.y>=1 && tmp.y<=8 && !vis[tmp.x][tmp.y])            {                tmp.step = now.step + 1;                tmp.g = now.g + gdis(abs(dir[i][0]), abs(dir[i][1]));                tmp.h = hdis(tmp, en);                tmp.f = tmp.g + tmp.h;                vis[tmp.x][tmp.y] = true;                que.push(tmp);            }        }    }}int main(){    char s1[10], s2[10];    while(scanf("%s%s", s1, s2)!=EOF)    {        node st, en;        st.x = s1[0]-'a'+1;        st.y = s1[1]-'0';        en.x = s2[0]-'a'+1;        en.y = s2[1]-'0';        int step = Astar(st,en);        printf("To get from %c%d to %c%d takes %d knight moves.\n",st.x+'a'-1,st.y,en.x+'a'-1,en.y,step);    }}


原创粉丝点击