【搜索题】
【题目】
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 Specification
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 Specification
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: University of Ulm Local Contest 1996
【题意说明】国际象棋中的“骑士”(我们俗称“马”,以下亦简称“马”)走的步法规则和中国象棋中的马类似,走“日”字格,如下图所示中“马”可走的位置为标号1(左上纵)、2(右上纵)、3(左上横)、4(右上横)、5(左下横)、6(右下横)、7(左下纵)、8(右下纵)。题目用字母a~h表示棋盘横坐标(指示棋盘纵列),数字1~8表示棋盘纵坐标(指示棋盘横列),则字母数字组合表示了棋盘的某个位置,如a1即表示棋盘左上角第一个位置。输入有多组用例,每组用例用上述的字母数字组合给出“马”的起始位置和目标位置,需输出“马”从起始位置到目标位置的最少步数。
【解答】
(一)分析:该题是典型的广度优先搜索问题。搜索树的节点包含了马当前的位置以及已走的步数,每个节点的后继节点即马从当前位置按照“日”字规则可行进的各个下一位置,从马的起始位置开始作为搜索树根(步数为0,该层为树的0层),后续同一层的节点的步数值即层数,为了避免重复搜索,用一个表格记录将要生成的节点是否已被搜索过,最后取第一次搜索生成目标位置的节点所在层数即马行走所需的最小步数。
(二)代码:
#include<iostream>
#include<queue>
using namespace std;
/* 搜索节点 */
typedef struct square
{
int i; //棋盘格子横坐标
int j; //棋盘格子纵坐标
int num; //行至该格子经历的步数
}Square;
/*************************************
* 主函数
*************************************/
int main()
{
queue<Square> q;//广搜队列
Square d;
int i,j,si,sj,ei,ej,ti,tj,tnum;
char s1[3],s2[3];
bool reach[8][8];//reach[i][j]表示格子(i,j)是否被搜索过
//针对每一组用例进行计算
while(cin>>s1>>s2)
{
for(i=0;i<=7;i++)
for(j=0;j<=7;j++)
reach[i][j]=false;
while(!q.empty())
q.pop();
//获得马的起始位置坐标(si,sj)以及步数0压入广搜队列
si=s1[0]-'a';//起始位置横坐标
sj=s1[1]-'1';//起始位置纵坐标
ei=s2[0]-'a';//目的位置横坐标
ej=s2[1]-'1';//目的位置纵坐标
d.i=si;
d.j=sj;
d.num=0;
q.push(d);//起始位置节点压入广搜队列
//进行广度优先搜索
while(!q.empty())
{
//弹出队头节点d
d=q.front();
q.pop();
ti=d.i;
tj=d.j;
tnum=d.num;
//该节点已经到达目的格子
if(ti==ei&&tj==ej)
{
cout<<"To get from ";
cout<<s1<<" to "<<s2;
cout<<" takes "<<tnum<<" knight moves."<<endl;
break;
}
//该节点代表的格子已经被搜索过
if(reach[ti][tj])
continue;
reach[ti][tj]=true;
//搜索当前节点位置的左上纵位置入广搜队列
if(ti-2>=0&&tj-1>=0)
{
d.i=ti-2;
d.j=tj-1;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的右上纵位置入广搜队列
if(ti-2>=0&&tj+1<=7)
{
d.i=ti-2;
d.j=tj+1;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的左上横位置入广搜队列
if(ti-1>=0&&tj-2>=0)
{
d.i=ti-1;
d.j=tj-2;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的右上横位置入广搜队列
if(ti-1>=0&&tj+2<=7)
{
d.i=ti-1;
d.j=tj+2;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的左下横位置入广搜队列
if(ti+1<=7&&tj-2>=0)
{
d.i=ti+1;
d.j=tj-2;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的右下横位置入广搜队列
if(ti+1<=7&&tj+2<=7)
{
d.i=ti+1;
d.j=tj+2;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的左下纵位置入广搜队列
if(ti+2<=7&&tj-1>=0)
{
d.i=ti+2;
d.j=tj-1;
d.num=tnum+1;
q.push(d);
}
//搜索当前节点位置的右下纵位置入广搜队列
if(ti+2<=7&&tj+1<=7)
{
d.i=ti+2;
d.j=tj+1;
d.num=tnum+1;
q.push(d);
}
}
}
return 0;
}
//Accepted
(解于2009/10)