• POJ2243 Knight Moves —— A*算法


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


    Knight Moves
    Time Limit: 1000MS   Memory Limit: 65536K
    Total 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 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就可以过了。但是想借助这题学一下A*算法(但以下写法好像不是完整的或者说正确的写法,先放一放,以后遇到再深入)。



    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #include <cmath>
     7 using namespace std;
     8 typedef long long LL;
     9 const int INF = 2e9;
    10 const LL LNF = 9e18;
    11 const int MOD = 1e9+7;
    12 const int MAXN = 8+10;
    13 
    14 struct node
    15 {
    16     int x, y, step;
    17     int g, h, f;
    18     bool operator<(const node&a)const{
    19         return f>a.f;
    20     }
    21 };
    22 bool vis[MAXN][MAXN];
    23 int dir[8][2] = { {1,2}, {1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1} };
    24 
    25 int gdis(int x_step, int y_step)    //直线距离
    26 {
    27     return (int)sqrt(x_step*x_step+y_step*y_step)*10+1;
    28 }
    29 
    30 int hdis(node a, node b)    //曼哈顿距离
    31 {
    32     return ( abs(a.x-b.x) + abs(a.y-b.y) )*10;
    33 }
    34 
    35 priority_queue<node>que;
    36 int Astar(node st, node en)
    37 {
    38     memset(vis,false,sizeof(vis));
    39     st.step = st.g = st.h = st.f = 0;
    40     while(!que.empty()) que.pop();
    41     que.push(st);
    42     vis[st.x][st.y] = true;
    43 
    44     while(!que.empty())
    45     {
    46         node now = que.top();
    47         que.pop();
    48         if(now.x==en.x && now.y==en.y)
    49             return now.step;
    50 
    51         for(int i = 0; i<8; i++)
    52         {
    53             node tmp;
    54             tmp.x = now.x + dir[i][0];
    55             tmp.y = now.y + dir[i][1];
    56             if(tmp.x>=1 && tmp.x<=8 && tmp.y>=1 && tmp.y<=8 && !vis[tmp.x][tmp.y])
    57             {
    58                 tmp.step = now.step + 1;
    59                 tmp.g = now.g + gdis(abs(dir[i][0]), abs(dir[i][1]));
    60                 tmp.h = hdis(tmp, en);
    61                 tmp.f = tmp.g + tmp.h;
    62 
    63                 vis[tmp.x][tmp.y] = true;
    64                 que.push(tmp);
    65             }
    66         }
    67     }
    68 }
    69 
    70 int main()
    71 {
    72     char s1[10], s2[10];
    73     while(scanf("%s%s", s1, s2)!=EOF)
    74     {
    75         node st, en;
    76         st.x = s1[0]-'a'+1;
    77         st.y = s1[1]-'0';
    78         en.x = s2[0]-'a'+1;
    79         en.y = s2[1]-'0';
    80         int step = Astar(st,en);
    81         printf("To get from %c%d to %c%d takes %d knight moves.
    ",st.x+'a'-1,st.y,en.x+'a'-1,en.y,step);
    82     }
    83 }
    View Code


  • 相关阅读:
    mysql 查看并修改默认端口号
    安装MySQL-python 的问题
    mysql安装使用
    SAE部署Django1.6+MySQL
    Yii2框架学习 4-2小部件学习 (DetailView、ActiveForm、GridView 、ActionColumn 动作列的配置属性)
    Yii2框架学习 3-4 查询构建器QueryBuilder 的学习
    Yii2框架学习 4-2 HTML助手类、 Array Helper助手类
    Yii2框架学习 3-3 单条数据详情展示; 数据关联查询时 hasOne和hasMany
    Yii框架学习 3-2 数据库操作--ActiveRecord 操作和生命周期
    Yii2框架学习 3-1 yii连接数据库和使用数据库 数据库查询总结
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538602.html
Copyright © 2020-2023  润新知