zoukankan      html  css  js  c++  java
  • Hdu1372 Knight Moves (BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372

    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.

    宽度优先搜索,只是搜索方向和一般的上下左右的有点不同,是一个类似于象棋中的马的移动方式,所以有八个方向,dx[8]和dy[8]记录八个方向,输入搞了会儿。。。

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cmath> 
     4 #include <string>
     5 #include <cstdio> 
     6 #include <cstring>
     7 #include <queue>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 typedef pair<int,int> P;
    11 char a[10][10];
    12 int n=8;
    13 int dx[8]={1,2,2,1,-1,-2,-2,-1},dy[8]={2,1,-1,-2,-2,-1,1,2};
    14 int sx,sy,gx,gy;
    15 int d[10][10];
    16 char c[6]; 
    17 void init()
    18 {
    19     for(int i=0;i<8;i++){
    20         for(int j=0;j<8;j++){
    21             a[i][j]='.';
    22         } 
    23     }
    24 }
    25 int bfs()
    26 {
    27     memset(d,INF,sizeof(d));
    28     d[sx][sy]=0;
    29     queue<P> que;
    30     que.push(P(sx,sy));
    31     while(!que.empty()){
    32         P p=que.front();
    33         que.pop();
    34         int x=p.first,y=p.second;
    35         if(x==gx&&y==gy) break;
    36         for(int i=0;i<8;i++){
    37             int nx=x+dx[i],ny=y+dy[i];
    38             if(nx>=0&&nx<n&&ny>=0&&ny<n&&a[nx][ny]!='#'&&d[nx][ny]==INF){
    39                 que.push(P(nx,ny));
    40                 d[nx][ny]=d[x][y]+1;
    41             }
    42         }
    43     }
    44     return d[gx][gy];
    45 }
    46 int main()
    47 {
    48     init();
    49     while(gets(c)){
    50         sx=c[0]-'a',sy=c[1]-'0'-1;
    51         gx=c[3]-'a',gy=c[4]-'0'-1;
    52         cout<<"To get from "<<c[0]<<c[1]<<" to "<<c[3]<<c[4]<<" takes "<<bfs()<<" knight moves."<<endl;
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    美团面试,360面试 ,滴滴面试,阿里面试,百度面试,京东面试,搜狗面试:
    Maven 3-Maven依赖版本冲突的分析及解决小结 (阿里,美团,京东面试)
    maven snapshot和release版本的区别
    Maven 生命周期 和插件
    Maven pom 文件解释
    Zookeeper原理架构
    sublime 支持PHP语法提示
    Zen Coding 用法
    让浏览器屏蔽js
    淘宝设计师入门:设计师SDK环境配置
  • 原文地址:https://www.cnblogs.com/shixinzei/p/10747554.html
Copyright © 2011-2022 走看看