• 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!!
  • 相关阅读:
    zech的神秘题库(武汉理工夜莺杯)
    回归第六题
    同余方程
    牛牛选路径(牛客)
    回归第三题
    区间dp复习提高专题
    乘法逆元(线性递推)
    回归第八题
    JAVA启动参数大全之二:非标准参数(转)
    (转)Spring Security 3.1 自定义实例之登陆
  • 原文地址:https://www.cnblogs.com/pingge/p/3132723.html
Copyright © 2020-2023  润新知