Knight Moves ZOJ - 1091

题目

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

1
2
3
4
5
6
7
8
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

1
2
3
4
5
6
7
8
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,从出发点开始到结束点搜索最短路径。用map算出出发点容易点吧。
马走日字形。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<string>
#include<map>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int mp[9][9];
int visit[9][9];
map<char,int> map1;
map<char,int> map2;
int sx=0,sy=0,ex=0,ey=0;
int dx[]={-1,-2,-2,-1,1,2,2,1};
int dy[]={-2,-1,1,2,2,1,-1,-2};
struct node{
int x,y,dep;
node(int x,int y,int dep):x(x),y(y),dep(dep){}
};
int ans=0;
void BFS(int x,int y){
memset(visit,0,sizeof(visit));
queue<node> qu;
qu.push(node(x,y,0));
visit[x][y]=1;
while(!qu.empty()){
node t=qu.front();qu.pop();
for(int i=0;i<8;i++){
int tx=t.x+dx[i];
int ty=t.y+dy[i];
if(tx<1||tx>8||ty<1||ty>8||visit[tx][ty]==1) continue;
qu.push(node(tx,ty,t.dep+1));
if(tx==ex&&ty==ey) {ans=t.dep+1;return;}
visit[tx][ty]=1;
}
}
}
int main(){
string str1=" abcdefgh";
string str2=" 12345678";
for(int i=1;i<9;i++){
map1[str1[i]]=i;
map2[str2[i]]=i;
}
// for(map<char,int>::iterator it=map2.begin();it!=map2.end();it++)
// cout<<it->first<<' '<<it->second<<endl;
while(cin>>str1>>str2){
ans=0;
sx=map1[str1[0]];
sy=map2[str1[1]];
ex=map1[str2[0]];
ey=map2[str2[1]];
// cout<<sx<<' '<<sy<<' '<<ex<<' '<<ey<<endl;
BFS(sx,sy);
cout<<"To get from "<<str1<<" to "<<str2<<" takes "<<ans<<" knight moves."<<endl;
}
return 0;
}
-------------本文结束感谢您的阅读-------------
0%