zoukankan      html  css  js  c++  java
  • HDU_1372——骑士移动,二维空间BFS

    基本BFS搜索

    Problem 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 file 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.
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 #define MAX 8
     5 using namespace std;
     6 struct node
     7 {
     8    int x,y,move;   
     9 }start;
    10 char str1[MAX],str2[MAX];
    11 int map[MAX][MAX];
    12 int dir[8][2]={{2,1},{1,2},
    13                {2,-1},{-1,2},
    14                {-2,1},{1,-2},
    15                {-2,-1},{-1,-2}
    16               };           
    17 int Bfs(void)
    18 {
    19    queue<node>p;
    20    p.push(start);
    21    node temp,next;
    22    while(!p.empty())
    23       {
    24          temp=p.front();
    25          p.pop();
    26          if(map[temp.x][temp.y]==2)
    27             return temp.move;
    28          next.move=temp.move+1;
    29          for(int i=0;i<8;i++)
    30             {
    31                next.x=temp.x+dir[i][0];
    32                next.y=temp.y+dir[i][1];
    33                if(next.x>=0&&next.x<MAX && next.y>=0&&next.y<MAX && map[next.x][next.y]!=1)
    34                   {
    35                      if(map[next.x][next.y]==2)    return next.move;
    36                      map[next.x][next.y]=1;
    37                      p.push(next);
    38                   }   
    39             }   
    40       }
    41    return 0;
    42 }
    43 
    44 int main()
    45 {
    46    int ans;
    47    while(~scanf("%s%s",str1,str2))
    48       {
    49          memset(map,0,sizeof(map));
    50          start.x=str1[0]-'a';start.y=str1[1]-'1';start.move=0;
    51          map[str2[0]-'a'][str2[1]-'1']=2;
    52          printf("To get from %s to %s takes %d knight moves.\n",str1,str2,Bfs());
    53       }
    54    return 0;   
    55 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    OJ:自己实现一个简单的 priority_queue
    OJ:访问 const 成员函数问题
    OJ:重载 << 运算符
    Qt 编程中 namespace Ui { class Widget; } 解析
    QT 实现图片旋转的两种方法
    QTimer 的使用
    QT 完美实现圆形按钮
    QT 设置有效绘图区域
    基于 LWIP 建立 TCP Server 与主机通信实验
    大整数相乘 分治法 和 循环暴力法
  • 原文地址:https://www.cnblogs.com/pingge/p/3132723.html
Copyright © 2011-2022 走看看