zoukankan      html  css  js  c++  java
  • 4.2.3 Knight Moves

    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 <cmath>
      3 #include <queue>
      4 #include <iostream>
      5 #include <cstdlib>
      6 using namespace std;
      7 
      8 struct qq
      9 {
     10 int x,y;
     11 };
     12 queue<qq> q;
     13 
     14 const int dx[]={-1,-1,-2,-2,1,1,2,2};
     15 const int dy[]={-2,2,-1,1,-2,2,-1,1};
     16 bool vis[9][9],flag=false;
     17 char s1[3],s2[3];
     18 int findx,findy,ax,ay,xx,yy,step,l;
     19 qq s,ya;
     20 
     21 void close()
     22 {
     23 //fclose(stdin);
     24 //fclose(stdout);
     25 exit(0);
     26 }
     27 
     28 void judge()
     29 {
     30     for (int j=0;j<8;j++)
     31     {
     32         xx=dx[j]+s.x;
     33         yy=dy[j]+s.y;
     34     //    printf("dx:%d dy:%d xx:%d yy:%d j:%d \n",dx[j],dy[j],xx,yy,j);
     35         if (xx==findx && yy==findy) { flag=true; return; }
     36           if (xx>0 && xx<9 && yy>0 && yy<9 && vis[xx][yy]==false)  //边界检查及判重
     37           {
     38               vis[xx][yy]=true; //这一步标记一定不能忘了
     39               ya.x=xx; ya.y=yy;
     40               q.push(ya);
     41           }
     42     }
     43 }
     44 
     45 void bfs()
     46 {
     47 step=0;
     48 memset(vis,false,sizeof(vis));
     49 flag=false;
     50 while (!q.empty())
     51     q.pop();
     52     s.x=ax; s.y=ay;
     53      q.push(s);
     54      while (!q.empty())
     55      {
     56          step++;
     57          l=q.size();
     58          for (int i=0;i<l;i++)
     59          {
     60              s=q.front();
     61              q.pop();
     62         //     printf("-------s.x:%d s.y:%d--------\n",s.x,s.y);
     63                 judge();
     64                  if (flag) 
     65                  {
     66                   printf("To get from %s to %s takes %d knight moves.\n",s1,s2,step);
     67                      return ;
     68                  }
     69          }
     70      }
     71 }
     72 
     73 
     74 
     75 
     76 void init()
     77 {
     78 //freopen("knight.in","r",stdin);
     79 //freopen("knight.out","w",stdout);
     80   while (scanf("%s%s",s1,s2)!=EOF)
     81   {
     82       ax=s1[0]-96;  //小写字母
     83       ay=s1[1]-48;
     84       findx=s2[0]-96;
     85       findy=s2[1]-48;
     86     //  printf("ax:%d ay:%d findx:%d findy:%d \n",ax,ay,findx,findy);
     87       //printf("s1:%s s2:%s\n",s1,s2);
     88       if (strcmp(s1,s2)==0)
     89       {
     90           printf("To get from %s to %s takes 0 knight moves.\n",s1,s2);
     91           continue;
     92       }
     93       bfs();
     94   }
     95 }
     96 
     97 
     98 
     99 int main ()
    100 {
    101 init();
    102 //bfs();
    103 close();
    104 return 0;
    105 }
  • 相关阅读:
    linux下删除乱码文件
    linux修改网卡延时、带宽、丢包等
    连接远程系统里的vbox虚拟机
    Linux路由功能
    关于C语言的取整函数
    新博客
    NEU1217之神风堂最少人数 自己在精度上吃了苦头
    sprintf sscanf自己应该会的小点
    【转】妙解亲和数
    redeclared as different kind of symbol
  • 原文地址:https://www.cnblogs.com/cssystem/p/2831664.html
Copyright © 2011-2022 走看看