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 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.
题解
题目的大意是给你一个棋盘,要求马从起点到终点需要的最短距离。
对于暴力搜索最短距离,推荐广度优先搜索。
马只能走日字。假设马起点在{0,0},那么他可以往8个方向跳({1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}),对于该8个方向的拓展要判断是否超出边界,然后还要判断该点是否重复来过。
代码
BFS通解
对于BFS函数里的队列操作,具体过程:起点入队列,队头取元素,拓展,入队列。
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct position
{
int x,y;
}dir[8]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
//定义方向数组dir,初值为8个方向的相对坐标偏移量
int d[4];//d[0],d[1]为起始点,d[2],d[3]为终止点
bool vis[9][9];//访问标记数组vis
bool in(int a,int b)//检查坐标(a,b)是否合法
{
if(a>8||a<1)
return false;
if(b>8||b<1)
return false;
if(vis[a][b])
return false;
return true;
}
int BFS()
{
int col,row;
int ans=0;
queue<int>que;
que.push(d[0]);//起点列号col入队列
que.push(d[1]);//起点行号col入队列
que.push(ans);//起点布号col入队列
vis[d[0]][d[1]]=1;//起点置访问标记为true(1)
while(!que.empty())
{
col=que.front();que.pop();
row=que.front();que.pop();
ans=que.front();que.pop();
if(col==d[2]&&row==d[3])
return ans;//到达目标位置
for(int k=0;k<8;k++)//从八个方向进行拓展,满足条件即加入队列
{
if(in(col+dir[k].x,row+dir[k].y)){
que.push(col+dir[k].x);
que.push(row+dir[k].y);
que.push(ans+1);
vis[col+dir[k].x][row+dir[k].y]=1;
}
}
}
}
int main()
{
//freopen("123.txt","r",stdin);
char c[6];
while(gets(c))
{
for(int i=1;i<9;i++)//初始化
for(int j=1;j<9;j++)
vis[i][j]=0;
d[0]=c[0]-'a'+1;//起点列,并把字母转化为数字形式
d[1]=c[1]-'0';//起点行
d[2]=c[3]-'a'+1;//终点列
d[3]=c[4]-'0';
int ans=BFS();
printf("To get from %c%c to %c%c takes %d knight moves.\n",c[0],c[1],c[3],c[4],ans);
}
//fclose(stdin);
return 0;
}
双向BFS
- DBFS算法是对BFS算法的一种扩展。
- DBFS算法从两个方向以广度优先的顺序同时扩展,一个是从起始节点开始扩展,另一个是从目的节点扩展,直到一个扩展队列中出现另外一个队列中已经扩展的节点,也就相当于两个扩展方向出现了交点,那么可以认为我们找到了一条路径。
- 总之从起点、终点同时开始,所以搜索树的深度得到了明显的减少。
- 相遇时,ans=正向步+反向步
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
struct position
{
int x,y;
}dir[8]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
//定义方向数组dir,初值为8个方向的相对坐标偏移量
int d[4];//d[0],d[1]为起始点,d[2],d[3]为终止点
int vis[9][9];//访问标记数组vis
int ans,ans1,ans2;
queue<int>que1,que2;
bool in(int a,int b,int c)//检查坐标(a,b)是否合法
{
if(a>8||a<1||b>8||b<1)
return false;
if(vis[a][b]==c)
return false;
return true;
}
void DBFS()
{
int col,row,stp,cnt;
bool flag=true;//双向BFS拓展时未相遇标记。默认true,相遇则置为false
if(d[0]==d[2] &&d[1]==d[3]) return;
que1.push(d[0]);que1.push(d[1]);que1.push(0);
vis[d[1]][d[0]]=1;
que2.push(d[2]);que2.push(d[3]);que2.push(0);
vis[d[3]][d[2]]=2;
while(flag){//先正向再方向逐层拓展
cnt=que1.size()/3;//统计本层结点个数,循环完该层即结束
while(cnt-- && flag){
col=que1.front();que1.pop();取队头
row=que1.front();que1.pop();
stp=que1.front();que1.pop();
for(int k=0;k<8;k++)//从八个方向进行拓展,满足条件即加入队列
{
if(in(row+dir[k].y,col+dir[k].x,1)){
ans1=stp+1;
if(vis[row+dir[k].y][col+dir[k].x]==2){
flag=false;
ans=ans1+ans2;
break;
}
que1.push(col+dir[k].x);que1.push(row+dir[k].y);que1.push(ans1);
vis[row+dir[k].y][col+dir[k].x]=1;
}
}
}
cnt=que2.size()/3;//统计本层结点个数,循环完该层即结束
while(cnt-- && flag){
col=que2.front();que2.pop();//取队头
row=que2.front();que2.pop();
stp=que2.front();que2.pop();
for(int k=0;k<8;k++)//从八个方向进行拓展,满足条件即加入队列
{
if(in(row+dir[k].y,col+dir[k].x,2)){
ans2=stp+1;
if(vis[row+dir[k].y][col+dir[k].x]==1){
flag=false;
ans=ans1+ans2;
break;
}
que2.push(col+dir[k].x);que2.push(row+dir[k].y);que2.push(ans2);
vis[row+dir[k].y][col+dir[k].x]=2;
}
}
}
}
}
int main()
{
//freopen("123.txt","r",stdin);
char c[6];
while(gets(c))
{
while(!que1.empty()) que1.pop();
while(!que2.empty()) que2.pop();
for(int i=1;i<9;i++)//初始化
for(int j=1;j<9;j++)
vis[i][j]=0;
d[0]=c[0]-'a'+1;//起点列,并把字母转化为数字形式
d[1]=c[1]-'0';//起点行
d[2]=c[3]-'a'+1;//终点列
d[3]=c[4]-'0';//终点行
ans=ans1=ans2=0;
DBFS();
printf("To get from %c%c to %c%c takes %d knight moves.\n",c[0],c[1],c[3],c[4],ans);
}
//fclose(stdin);
return 0;
}