zoukankan      html  css  js  c++  java
  • HDU 1372

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

    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.

    谨以此纪念一下自己学会的第一道BFS~

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 using namespace std;
     5 struct type{
     6     int x,y,step;
     7 }st,ed;
     8 bool s[10][10];
     9 int dir[8][2] = {{-1,+2},{-2,+1},{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2}};
    10 
    11 int bfs()
    12 {
    13     queue<type> q;
    14     memset(s,0,sizeof(s));
    15     s[st.x][st.y]=1;
    16     q.push(st);
    17     while(!q.empty())
    18     {
    19         type next,now=q.front();q.pop();
    20         //printf("now: x=%d y=%d step=%d
    ",now.x,now.y,now.step);
    21         if(now.x == ed.x && now.y == ed.y) return now.step;
    22         for(int i=0;i<8;i++)
    23         {
    24             next.x=now.x+dir[i][0];
    25             next.y=now.y+dir[i][1];
    26             next.step=now.step+1;
    27             if(next.x>=1 && next.y>=1 && next.x<=8 && next.y<=8 && s[next.x][next.y]==0)
    28             {
    29                 s[next.x][next.y]=1;
    30                 q.push(next);
    31             }
    32         }
    33     }
    34 }
    35 
    36 int main()
    37 {
    38     char begin[2],end[2];
    39     while(scanf("%c%c %c%c%*c",&begin[0],&begin[1],&end[0],&end[1])!=EOF)
    40     {
    41         st.x=begin[1]-'0';
    42         st.y=begin[0]-'a'+1;
    43         st.step=0;
    44         //printf("st: x=%d y=%d
    ",st.x,st.y);
    45         ed.x=end[1]-'0';
    46         ed.y=end[0]-'a'+1;
    47         //printf("ed: x=%d y=%d
    ",ed.x,ed.y);
    48         int ans=bfs();
    49         printf("To get from %c%c to %c%c takes %d knight moves.
    ",begin[0],begin[1],end[0],end[1],ans);
    50     }
    51 }


  • 相关阅读:
    java8学习之Collector复合与注意事项
    动画学习之WIFI图形绘制
    java线程基础巩固---多线程死锁分析,案例介绍
    java8学习之Collector同一性与结合性分析
    java8学习之Collector源码分析与收集器核心
    java8学习之Stream分组与分区详解
    kotlin面向对象之抽象类、继承、多态
    matplotlib-曲线和折线案例
    人口、人口密度分析项目-条形图
    开机自启:bat实现一次性打开win7中的常用软件和文件夹
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804163.html
Copyright © 2011-2022 走看看