4.2.3 Knight Moves

本文介绍了一种使用广度优先搜索(BFS)算法解决骑士在国际象棋棋盘上从一点到另一点的最短行走路径的问题。通过具体示例展示了如何实现这一算法,并给出了完整的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

转载于:https://www.cnblogs.com/cssystem/archive/2012/12/24/2831664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值