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的大致流程:
Q.push(first)
while(!Q.empty()){
tmp=Q.front();Q.pop();
for(拓展下一步){
if(合法状态){
if(目标节点){
输出;
return;
}
if(未拓展){
Q.push(new);
标记new为已拓展;
}
}
}
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct state{
int col;
int row;
int step;
};
queue<state> Q;
string str;
int col,row,goal_col,goal_row;
int dx[8]={-2,-2,-1,-1,+1,+1,+2,+2},
dy[8]={-1,+1,-2,+2,-2,+2,-1,+1};
int hash0[100];
void bfs(){
while(!Q.empty()){
state tmp=Q.front();Q.pop();
//cout<<tmp.col<<' '<<tmp.row<<' '<<tmp.step<<endl;;
for(int i=0;i<8;i++){
if ((tmp.col+dx[i]<=8)&&(tmp.col+dx[i]>=1)&&(tmp.row+dy[i]<=8)&&(tmp.row+dy[i]>=1)){
if ((tmp.col+dx[i]==goal_col)&&(tmp.row+dy[i]==goal_row)){
printf("To get from %c%c to %c%c takes %d knight moves.\n",str[0],str[1],str[3],str[4],tmp.step+1);
return;
}
if (hash0[10*(tmp.col+dx[i])+(tmp.row+dy[i])]==0){
state tmp1={tmp.col+dx[i],tmp.row+dy[i],tmp.step+1};
Q.push(tmp1);
hash0[10*(tmp1.col)+tmp1.row]=1;
}
}
}
}
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(getline(cin,str)){
memset(hash0,0,sizeof(hash0));
while(!Q.empty()) Q.pop();
col=str[0]-'a'+1;
row=str[1]-'0';
goal_col=str[3]-'a'+1;
goal_row=str[4]-'0';
if ((col==goal_col)&&(row==goal_row)){
printf("To get from %c%c to %c%c takes %d knight moves.\n",str[0],str[1],str[3],str[4],0);
continue;
}
hash0[col*10+row]=1;
state first={col,row,0};
Q.push(first);
bfs();
}
return 0;
}