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!!
  • 相关阅读:
    一步一步学习IdentityServer4 (4) 处理特殊需求之-登录等待页面
    php 打包下载
    nginx https反向代理tomcat
    the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
    layui配置
    vue 学习一
    MyCAT+MySQL 搭建高可用企业级数据库集群——第3章 MyCat核心配置讲解
    第一模块·开发基础-第3章 作业讲解
    《扭转人生的40个哲学提问》 徐帆 著
    零基础学 JavaScript 全彩版 明日科技 编著
  • 原文地址:https://www.cnblogs.com/pingge/p/3132723.html
Copyright © 2011-2022 走看看