zoukankan      html  css  js  c++  java
  • ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 Specification

    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 Specification

    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.

    题目大意:国际象棋的骑士走日。在一个8*8的棋盘中,给定两个点,求骑士从一个点到达另一个点的最少步数。

    BFS题目,代码如下:
     1 # include<iostream>
     2 # include<cstdio>
     3 # include<queue>
     4 # include<cstring>
     5 using namespace std;
     6 bool vis[8][8];        //可以大幅度缩短搜索时间,本题不加这个也可以AC
     7 int dx[8] = {1,1,-1,-1,2,-2,2,-2};
     8 int dy[8] = {2,-2,2,-2,1,1,-1,-1};
     9 int sx,sy,ex,ey;
    10 bool ismap(int x,int y){
    11     if(x<0 || y<0 ||x>=8 || y>=8)
    12         return false;
    13     return true;
    14 }
    15 
    16 struct Node{
    17     int x,y,step;
    18 }qishi;
    19 
    20 queue<Node>q;
    21 
    22 int bfs(){
    23     memset(vis,0,sizeof(vis));
    24     qishi.x = sx; qishi.y = sy; qishi.step = 0;
    25     while(!q.empty()) q.pop();
    26     vis[sx][sy] = 1;
    27     q.push(qishi);
    28 
    29     while(!q.empty()){
    30         Node tmp = q.front();    q.pop();      
    31         if(tmp.x==ex && tmp.y == ey)
    32             return tmp.step;
    33         for(int i=0;i<8;i++){
    34             int x = tmp.x + dx[i];
    35             int y = tmp.y + dy[i];
    36             if(vis[x][y]) continue;
    37             if(!ismap(x,y)) continue;
    38             vis[x][y] = 1;
    39             qishi.x = x;    
    40             qishi.y = y;
    41             qishi.step = tmp.step + 1;
    42             q.push(qishi);
    43         }
    44     }
    45 }
    46 
    47 int main(){
    48     char a[5],b[5];
    49     while(scanf("%s%s",a,b)!=EOF){
    50         sx = a[0]-'a';   sy = a[1]-'1';
    51         ex = b[0]-'a';   ey = b[1]-'1';
    52         printf("To get from %s to %s takes %d knight moves.
    ", a, b, bfs());
    53     }
    54     return 0;
    55 }
     
  • 相关阅读:
    2020-08-11 题目题解
    N皇后问题
    逆序对(模板)
    归并选择(模板)
    快速选择(模板)
    快速排序(模板)
    vuecli4+elementui实现面包屑
    vue-路由导航(守卫)那些事
    vue-vant中ImagePreview 图片预览正确的打开方式
    Selenium 对表格table处理
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3213510.html
Copyright © 2011-2022 走看看