# HDU 1372 Knight Moves

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.
InputThe 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.
OutputFor 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.

### 1．由原始问题引出的3个问题

（1）求任意两点间的最短步数

（2）求任意两点间的所有最短路径及其全部信息

（3）骑士周游世界问题

### 2．问题分析

（1）棋盘的表示方法

（2）马的跳跃方向

https://blog.csdn.net/livelylittlefish/article/details/2108045

#include<stdio.h>//knight moves
#include<string.h>
#include<queue>
#define M 10
using namespace std;
int ma[M][M];

int ne[8][2]= {{1,-2},{2,-1},{2,1},{1,2},//马走日的八个方向.
{-1,2},{-2,1},{-2,-1},{-1,-2}
};

struct node
{
int x;
int y;
int step;
};

int main()
{
int ex,ey;
char st[3],ed[3];
while(~scanf("%s%s",st,ed))
{
memset(ma,0,sizeof(ma));

ex=ed[0]-'a';//转化终点.并记录
ey=ed[1]-'1';

struct node n;//转化起点,并且准备入队首.
n.x=st[0]-'a';
n.y=st[1]-'1';
n.step=0;//记得初始化起点的步数.
ma[n.x][n.y]=0;//这个也是
queue<node> q;
q.push(n);

//        for(int i=0;i<8;i++)//搜索前的棋盘步数格局(检验bug的时候看)
//        {
//            for(int j=0;j<8;j++)
//                printf("%d ",ma[i][j]);
//            printf("\n");
//        }

while(!q.empty())
{
//            printf("1\n");//看看有没有死循环,在修bug 的时候
node n=q.front();
q.pop();

if(n.x==ex&&n.y==ey)//如果走到了终点
{
printf("To get from %s to %s takes %d knight moves.\n",st,ed,n.step);
break;//记得跳出循环,记得跳出循环,记得跳出循环
}

for(int i=0; i<8; i++)
{
struct node nn;
nn.x=n.x+ne[i][0];
nn.y=n.y+ne[i][1];
if(nn.x<0||nn.x>=8||nn.y<0||nn.y>=8||ma[nn.x][nn.y]!=0)//如果这个点不在棋盘里或者这个点已经被走过
continue;
else//如果满足在棋盘,并且没走过
{
nn.step=n.step+1;//每次扩展一次方向,则增加一步.
ma[nn.x][nn.y]=nn.step;//标记步数在棋盘里.(这步其实只要标记一下就好了,这为了方便看他是怎么走的 步数的动态)
q.push(nn);//入队列.
}
}
}

//        for(int i=0;i<8;i++)//队列搜索完后的棋盘步数格局(检验bug的时候看)
//        {
//            for(int j=0;j<8;j++)
//                printf("%d ",ma[i][j]);
//            printf("\n");
//        }

while(!q.empty())//清空队列;
q.pop();
}
return 0;//这是个好习惯
}

ps:2018-04-16 21:23:14

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120