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.
There are multiple test cases. The first line contains an integer T, indicating the number of 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.
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
8 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.
// Problem#: 1936
// Submission#: 2199534
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <queue>
#include <algorithm>
#include <iostream>
#include <memory.h>
using namespace std;
int direction[][2]={
-2,-1,
-2,1,
-1,-2,
-1,2,
1,-2,
1,2,
2,-1,
2,1
};
bool visit[9][9];
int coun;
void bfs(int first1,int first2,int second1,int second2)
{
if(first1==second1&&first2==second2)
{
cout<<"To get from "<<(char)(first1+'a')<<first2+1<<" to "<<(char)(second1+'a')<<second2+1<<" takes "<<0<<" knight moves."<<endl;
return ;
}
memset(visit,0,sizeof(visit));
queue<pair<pair<int,int> , int > > qu;
visit[first1][first2]=1;
qu.push(make_pair(make_pair(first1,first2),0));
coun=0;
bool find=0;
while(!qu.empty())
{
pair<pair<int,int> , int > a=qu.front();
qu.pop();
for (int i = 0; i < 8; ++i)
{
if(a.first.first+direction[i][0]>=0&& a.first.first+direction[i][0] <8 && a.first.second+direction[i][1]>=0 && a.first.second+direction[i][1]< 8)
{
if(!visit[a.first.first+direction[i][0]][a.first.second+direction[i][1]])
{
//cout<<a.first.first+direction[i][0]<<a.first.second+direction[i][1]<<endl;
visit[a.first.first+direction[i][0]][a.first.second+direction[i][1]]=1;
qu.push(make_pair(make_pair(a.first.first+direction[i][0],a.first.second+direction[i][1]),a.second+1));
}
if(a.first.first+direction[i][0]== second1 && a.first.second+direction[i][1] == second2)
{
coun=a.second;
find=1;
break;
}
}
}
if(find)
{
cout<<"To get from "<<(char)(first1+'a')<<first2+1<<" to "<<(char)(second1+'a')<<second2+1<<" takes "<<coun+1<<" knight moves."<<endl;
break;
}
}
}
int main(int argc, char const *argv[])
{
int casen;
cin>>casen;
while(casen--)
{
char temp1,temp2;
int a,b,c,d;
cin>>temp1>>b>>temp2>>d;
a=temp1-'a';
c=temp2-'a';
bfs(a,b-1,c,d-1);
}
return 0;
}
这一道题从2点多开始写,一开始就觉得很简单,因为只是一次bfs的搜索过程啊。可是做完,也就是实现了这个功能之后真的无力吐槽,首先要处理的是输入,输入完成之后,在队列里面pair嵌套pair,没有比这个还要蛋疼的事情了吧。a.first.first代表横坐标,a.first.second代表纵坐标。a.second 代表第几次的搜索。之前写的想错了,总体记录的变成了宽搜多少次了。a.second也可以叫做深度吧。就这么理解也行。