问题描述
你的一个朋友正在研究旅行骑士问题 (TKP),你将在其中找到最短的骑士移动封闭路径,该路径恰好访问棋盘上给定 n 个方格中的每个方格一次。他认为问题中最困难的部分是确定两个给定方格之间的最小骑士移动数,一旦完成了这一点,找到巡回赛将很容易。
你当然知道反之亦然。所以你让他写一个解决“困难”部分的程序。
你的工作是编写一个程序,它以两个正方形 a 和 b 作为输入,然后确定从 a 到 b 的最短路线上的骑士移动次数。
输入
输入文件将包含一个或多个测试用例。每个测试用例由一行组成,其中包含两个由一个空格分隔的正方形。正方形是一个字符串,由一个代表列的字母 (ah) 和代表棋盘上的行的数字 (1-8) 组成。
输出
对于每个测试用例,打印一行“To get from xx to yy takes n knight move.”。
样本输入
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
样本输出
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.
初始状态 起点坐标(st_x, st_y)
目标状态 终点坐标(ed_x, ed_y)
移动方式: r[8][2] = {
{1, 2}, {2, 1}, {-2, 1}, {-1, 2}, {1, -2}, {2, -1}, {-2, -1}, {-1, -2}}
利用规则(移动方式) 生成下一层的所有状态节点,并依次放入队列中
层层往下展开,直到到达终点
#include<bits/stdc++.h>
using namespace std;
int st_x, st_y, ed_x, ed_y;
char s1[3], s2[3]; // 注意字符串数组最后还要存一个\0,所以多一个位置
int vis[9][9], r[8][2] = {
{
1,