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 }
     
  • 相关阅读:
    angular组件开发
    APICloud案例源码、模块源码、考试源码、开发工具大集合!赶快收藏
    Vue-router实现单页面应用在没有登录情况下,自动跳转到登录页面
    原生js实现架子鼓特效
    A Beginner’s Introduction to CSS Animation中文版
    react开发教程(三)组件的构建
    体验javascript之美第五课 五分钟彻底明白 匿名函数自执行和闭包
    这些例子很炫,感兴趣的童鞋可以了解一下
    体验javascript之美6:如果你觉得什么都会了或者不知道js学什么了看这里-面向对象编程
    傅里叶变换,小波变换,EMD,HHT,VMD(经典和现代信号处理方法基本原理概念)
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3213510.html
Copyright © 2011-2022 走看看