zoukankan      html  css  js  c++  java
  • POJ 2243 Knight Moves

    Knight Moves
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13823   Accepted: 7719

    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

     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 struct knight {
     5     int x,y,step;
     6     int g,h,f;
     7     bool operator < (const knight & k) const {     
     8         return f > k.f;
     9     }
    10 } k;
    11 bool visited[8][8];                               
    12 int x1,y1,x2,y2,ans;                               //起点(x1,y1),终点(x2,y2),最少移动次数ans
    13 int dirs[8][2]= {{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}}; //8个移动方向
    14 priority_queue<knight> que;                        //最小优先级队列(开启列表)
    15 
    16 bool in(const knight & a) {                        //判断knight是否在棋盘内
    17     if(a.x<0 || a.y<0 || a.x>=8 || a.y>=8)
    18         return false;
    19     return true;
    20 }
    21 int Heuristic(const knight &a) {                   //manhattan估价函数
    22     return (abs(a.x-x2)+abs(a.y-y2))*10;
    23 }
    24 void Astar() {                                     //A*算法
    25     knight t,s;
    26     while(!que.empty()) {
    27         t=que.top(),que.pop(),visited[t.x][t.y]=true;
    28         if(t.x==x2 && t.y==y2) {
    29             ans=t.step;
    30             break;
    31         }
    32         for(int i=0; i<8; i++) {
    33             s.x=t.x+dirs[i][0],s.y=t.y+dirs[i][1];
    34             if(in(s) && !visited[s.x][s.y]) {
    35                 s.g =t.g + 23;                 //23表示根号5乘以10再取其ceil
    36                 s.h = Heuristic(s);
    37                 s.f = s.g + s.h;
    38                 s.step = t.step + 1;
    39                 que.push(s);
    40             }
    41         }
    42     }
    43 }
    44 int main() {
    45     char line[5];
    46     while(gets(line)) {
    47         x1=line[0]-'a',y1=line[1]-'1',x2=line[3]-'a',y2=line[4]-'1';
    48         memset(visited,false,sizeof(visited));
    49         k.x=x1,k.y=y1,k.g=k.step=0,k.h=Heuristic(k),k.f=k.g+k.h;
    50         while(!que.empty()) que.pop();
    51         que.push(k);
    52         Astar();
    53         printf("To get from %c%c to %c%c takes %d knight moves.
    ",line[0],line[1],line[3],line[4],ans);
    54     }
    55     return 0;
    56 }

    听人说A*算法很厉害(可以大幅度提高搜索的效率,快到你都想不到),但一听说要有一步预判,我就想就凭计算机那智商~~~~~-_-  -_-  -_-然而还是在今晚自学了一下A* 搜索,感觉还是不很不靠谱,时间复杂度:玄学(学长们都这么说~~QWQ)

  • 相关阅读:
    ehcache 2.4 即将发布,亮点多多
    2010 年个人回忆与总结
    ehcache 2.4 即将发布,亮点多多
    JBoss Seam 3.0.0.Beta1 发布
    JBoss Seam 3.0.0.Beta1 发布
    jQuery 1.5 正式版如期发布
    jQuery 1.5 正式版如期发布
    Contracts for Java
    2010 年个人回忆与总结
    Contracts for Java
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6399323.html
Copyright © 2011-2022 走看看