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.
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
题意:在一个8*8的棋盘上,输入两个字符串(代表两个棋的坐标),按照跳马的规则,问从第一个坐标的位置到第二个的最小步数是多少?
思路: 这道题用BFS解决,也是经典的模板题(bfs每一步求出来的都是最短的路径)
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=10;
int vis[N][N],sx,sy,tx,ty,ans;
int dxy[8][2]= {2,1,2,-1,-2,1,-2,-1,1,-2,1,2,-1,-2,-1,2};///dxy为方向数组(相对偏移量)
struct Node
{
int x,y,cnt;///x,y分别为横纵坐标,cnt记录起点到该位置所需的步数
};
bool in(int x,int y)///判断坐标(x,y)是否合法
{
if(x>0&&x<=8&&y>0&&y<=8) ///这里的范围要与主函数中的sx,tx,sy,ty范围匹配
return true;
return false;
}
queue<Node>que;
void bfs()
{
Node now,next;
now.x=sx,now.y=sy;
now.cnt=0;
vis[sx][sy]=1;
que.push(now);
while(!que.empty())
{
now=que.front();///now为队头
que.pop();///删除队头
if(now.x==tx&&now.y==ty) ///判断是否到达目标状态,若是则返回;不是,继续
{
ans=now.cnt;
return ;
}
for(int i=0; i<8; i++) ///八个方向
{
int nx=now.x+dxy[i][0],ny=now.y+dxy[i][1]; ///nx,ny为向8个方向中的一个方向走后的横纵坐标
if(in(nx,ny)&&!vis[nx][ny]) ///判断坐标是否合法并且该坐标没有标记
{
next.x=nx;
next.y=ny;
next.cnt=now.cnt+1;///步数的更新
que.push(next);///入队
vis[nx][ny]=1;///对此坐标进行标记
}
}
}
}
int main()
{
char str1[5],str2[5];
while(~scanf("%s%s",str1,str2))
{
while(!que.empty()) ///每次使队列为空以便下次输入时使用
que.pop();
memset(vis,0,sizeof(vis));///标记数组vis置0
sx=str1[0]-'a'+1; ///这里
tx=str2[0]-'a'+1; ///这里
sy=str1[1]-'0';
ty=str2[1]-'0';
bfs();
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,ans);
}
return 0;
}
双向BFS(交替逐层扩展)解法:
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int vis[10][10],sx,sy,tx,ty,flag;
int ans1,ans2,ans,num1,num2;
int dxy[8][2]= {2,1,2,-1,-2,1,-2,-1,1,-2,1,2,-1,-2,-1,2};
struct Node
{
int x,y,cnt;
};
queue<Node>que1,que2;
bool in(int x,int y)
{
if(x>0&&x<=8&&y>0&&y<=8)
return true;
return false;
}
void dbfs(queue <Node>&que,int m,int n,int &res,int an)
{
Node now,next;
int x,y,stp;
now=que.front();
x=now.x,y=now.y;
stp=now.cnt;
que.pop();
for(int i=0; i<8; i++)
{
int nx=now.x+dxy[i][0],ny=now.y+dxy[i][1];
if(in(nx,ny)&&vis[nx][ny]!=m)
{
res=stp+1;
if(vis[nx][ny]==n)
{
ans=res+an;
flag=0;
return ;
}
next.x=nx,next.y=ny;
next.cnt=res;
que.push(next);
vis[nx][ny]=m;
}
}
}
int main()
{
char str1[5],str2[5];
while(~scanf("%s%s",str1,str2))
{
while(!que1.empty())
que1.pop();
while(!que2.empty())
que2.pop();
memset(vis,0,sizeof(vis));
sx=str1[0]-'a'+1;
tx=str2[0]-'a'+1;
sy=str1[1]-'0';
ty=str2[1]-'0';
flag=1,ans1=ans2=ans=0;
Node now,nn;
now.x=sx,now.y=sy;
now.cnt=0;
que1.push(now);
vis[sx][sy]=1;
nn.x=tx,nn.y=ty;
nn.cnt=0;
que2.push(nn);
vis[tx][ty]=2;
if(sx==tx&&sy==ty)
printf("To get from %s to %s takes 0 knight moves.\n",str1,str2);
else
{
while(flag)
{
num1=que1.size();
while(num1--&&flag)
dbfs(que1,1,2,ans1,ans2);
num2=que2.size();
while(num2--&&flag)
dbfs(que2,2,1,ans2,ans1);
}
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,ans);
}
}
return 0;
}
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
int vis[10][10],sx,sy,tx,ty,flag;
int ans1,ans2,ans,num1,num2;
int dxy[8][2]= {2,1,2,-1,-2,1,-2,-1,1,-2,1,2,-1,-2,-1,2};
struct Node
{
int x,y,cnt;
};
queue<Node>que1,que2;
bool in(int x,int y)
{
if(x>0&&x<=8&&y>0&&y<=8)
return true;
return false;
}
void dbfs(queue <Node>&que,int m,int n,int &res,int an)
{
Node now,next;
int x,y,stp;
now=que.front();
x=now.x,y=now.y;
stp=now.cnt;
que.pop();
for(int i=0; i<8; i++)
{
int nx=now.x+dxy[i][0],ny=now.y+dxy[i][1];
if(in(nx,ny)&&vis[nx][ny]!=m)
{
res=stp+1;
if(vis[nx][ny]==n)
{
ans=res+an;
flag=0;
return ;
}
next.x=nx,next.y=ny;
next.cnt=res;
que.push(next);
vis[nx][ny]=m;
}
}
}
int main()
{
char str1[5],str2[5];
while(~scanf("%s%s",str1,str2))
{
while(!que1.empty())
que1.pop();
while(!que2.empty())
que2.pop();
memset(vis,0,sizeof(vis));
sx=str1[0]-'a'+1;
tx=str2[0]-'a'+1;
sy=str1[1]-'0';
ty=str2[1]-'0';
flag=1,ans1=ans2=ans=0;
Node now,nn;
now.x=sx,now.y=sy;
now.cnt=0;
que1.push(now);
vis[sx][sy]=1;
nn.x=tx,nn.y=ty;
nn.cnt=0;
que2.push(nn);
vis[tx][ty]=2;
if(sx==tx&&sy==ty)
printf("To get from %s to %s takes 0 knight moves.\n",str1,str2);
else
{
while(flag)
{
num1=que1.size();
while(num1--&&flag)
dbfs(que1,1,2,ans1,ans2);
num2=que2.size();
while(num2--&&flag)
dbfs(que2,2,1,ans2,ans1);
}
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,ans);
}
}
return 0;
}