zoukankan      html  css  js  c++  java
  • [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10542    Accepted Submission(s): 6211

    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.

     

     

    Source

    University of Ulm Local Contest 1996 

    原文大意:对一个国际象棋的棋盘,每次输入骑士(骑士=马)的起始位置和目标位置,问至少要多少步才能走到。

    解题思路:其实是一个裸的广搜,贴上充数用的,输入输出打麻烦了。

    #include<stdio.h>
    
    #include<string.h>
    
    #include<queue>
    
    using namespace std;
    
    struct mp
    
      {
    
           int x,y,step;
    
           mp(int x,int y,int step):x(x),y(y),step(step){}
    
      };
    
    queue<mp> q;
    
    void bfs(int xbegin,int ybegin,int xend,int yend)
    
      {
    
           const int move[2][8]={{1,2,2,1,-1,-2,-2,-1},{2,1,-1,-2,-2,-1,1,2}};
    
           int i,x,y,visit[9][9];
    
           char sans1,sans2;
    
           memset(visit,0,sizeof(visit));
    
           if(xbegin==xend&&ybegin==yend)
    
             {
    
                 sans1=xbegin+'a'-1;
    
                 sans2=xend+'a'-1;
    
                 printf("To get from %c%d to %c%d takes %d knight moves.
    ",sans1,ybegin,sans2,yend,0);
    
                 return;
    
              }
    
           while(!q.empty()) q.pop();
    
           q.push(mp(xbegin,ybegin,0));
    
           visit[xbegin][ybegin]=1;
    
           while(!q.empty())
    
             {
    
                 for(i=0;i<8;++i)
    
                   {
    
                    x=q.front().x+move[0][i];y=q.front().y+move[1][i];
    
                    if(x==xend&&y==yend)
    
                       {
    
                         sans1=xbegin+'a'-1;
    
                         sans2=xend+'a'-1;
    
                         printf("To get from %c%d to %c%d takes %d knight moves.
    ",sans1,ybegin,sans2,yend,q.front().step+1);
    
                         return;
    
                       }
    
                    if(x>0&&x<9&&y>0&&y<9&&(!visit[x][y]))
    
                     {
    
                                 q.push(mp(x,y,q.front().step+1));
    
                                 visit[x][y]=1;
    
                           }
    
                         }
    
                 q.pop();
    
              }
    
           return;
    
      }
    
    int main()
    
      {
    
           char s[5];
    
            int x_start,x_end,y_start,y_end;
    
           while(scanf("%s",&s)!=EOF)
    
             {
    
                 x_start=s[0]-'a'+1;
    
                 y_start=s[1]-'0';
    
                 scanf("%s",s);
    
                 x_end=s[0]-'a'+1;
    
                 y_end=s[1]-'0';
    
                 bfs(x_start,y_start,x_end,y_end);
    
              }
    
           return 0;
    
      }
    

      

  • 相关阅读:
    C#入门
    使用 OLEDB 及 SqlBulkCopy 将多个不在同一文件夹下的 ACCESS mdb 数据文件导入MSSQL
    aspose.word 读取word段落内容
    Jquery+Aajax 批量上传
    asp.net mvc web api Token验证
    iframe父页面和子页面获取元素和js变量
    JavaScrpt常用的封装方法
    ASP.NET MVC 导出Word报表
    Asp.net的对Excel文档的导入导出操作
    C++ 复制vector值到array,复制 array 到jintArray
  • 原文地址:https://www.cnblogs.com/fuermowei-sw/p/6127305.html
Copyright © 2011-2022 走看看