zoukankan      html  css  js  c++  java
  • HDU 1372 Knight Moves (bfs)

    Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


    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.
     
    题目大意
          在一个棋盘上(象棋),水平方向a--h竖直方向1--8,每组样例给定两个坐标,
          求象棋中的 ‘马’ 至少需要几步能够从位置一到达位置二。
    解题思路
          广搜,因为“马走日” 所以马在一个位置上最多能够走八个位置,
          定义一个八个位置的数组,每次搜索八个位置即可。
     
    AC代码:
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <queue>
     5 using namespace std;
     6 
     7 struct point
     8 {
     9     int x,y;
    10 };
    11 int p[10][10];     // 标记步数
    12 int sx,sy,dx,dy;   // 起始地点 和 终点
    13 int aa[8][2] = {-2,-1,-1,-2,1,-2,2,-1,2,1,1,2,-1,2,-2,1};  // 方向数组
    14 void bfs(int a,int b)
    15 {
    16     memset(p,0,sizeof(p));
    17     queue<point > que;
    18     point p1,p2,p3;
    19     p1.x = a; p1.y = b;      // 将起始点加入队列
    20     que.push(p1);
    21     p[a][b] = 0;
    22     if (a==dx && b==dy)      // 如果起始点和终点相同 直接输出
    23         return ;
    24     while (!que.empty())
    25     {
    26         p2 = que.front();
    27         for (int i = 0; i < 8; i ++)
    28         {
    29             p3.x = p2.x+aa[i][0];
    30             p3.y = p2.y+aa[i][1];
    31             if (p3.x>0 && p3.x<=8 && p3.y>0 && p3.y<=8)   // 判断边界
    32             {
    33                 if (!p[p3.x][p3.y])
    34                 {
    35                    que.push(p3);
    36                    p[p3.x][p3.y] = p[p2.x][p2.y]+1;
    37                    if (p3.x == dx && p3.y==dy)     // 如果到达终点直接输出
    38                         return ;
    39                 }
    40             }
    41         }
    42         que.pop();
    43     }
    44     return ;
    45 }
    46 int main ()
    47 {
    48     char s1[3],s2[3];
    49     while (scanf("%s%s",s1,s2)!=EOF)
    50     {
    51         sx = s1[0]-'a'+1;
    52         sy = s1[1]-'0';
    53         dx = s2[0]-'a'+1;
    54         dy = s2[1]-'0';
    55         bfs(sx,sy);
    56         printf("To get from %s to %s takes %d knight moves.
    ",s1,s2,p[dx][dy]);
    57     }
    58     return 0;
    59 }
     
  • 相关阅读:
    为什么不直接使用socket ,还要定义一个新的websocket 的呢
    js-权威指南-Web套接字
    CSS-蜂窝状展示区域(多个六边形)的一种实现方式
    MQTT入门介绍
    【珍惜时间】vuepro
    搭建react的vw架构时候报 Cannot load preset "advanced".
    跟我一起使用create-react-app脚手架搭建vw-layout解决方案
    【珍惜时间】iReport
    vue中 给router-view 组件的 绑定 key 的原因
    SQL Server Index详解
  • 原文地址:https://www.cnblogs.com/yoke/p/5929519.html
Copyright © 2011-2022 走看看