HDU 1372.Knight Moves

221

题目



Description  



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  



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  



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.


题解



国际象棋中,骑士(马)的行走方式是:


直走一个,再斜走一步(即1,2,-1,-2的组合)

如图(绿色为起始位置,红色为能够到达的位置)



由于读入数据中有字符,因此要注意避免错误读入回车(\n)


剩下就是标准标准的BFS模板


代码



/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)

const int maxn = 10;
int k[maxn];

const int delta[8] = {-1,-1,1,1,-2,2,2,-2};

int BFS(int s1,int s2,int v1,int v2) {
if(s1 == v1 && s2 == v2)
return 0;

queue<pair<int,int> > Q;
bool visited[maxn][maxn];
memset(visited,false,sizeof(visited));
int dis[maxn][maxn];
memset(dis,0,sizeof(dis));

Q.push(pair<int,int>(s1,s2));
visited[s1][s2] = true;

while(!Q.empty()) {
int th1 = Q.front().first;
int th2 = Q.front().second;
Q.pop();

//达到终点
if(th1 == v1 && th2 == v2)
break;

//拓展节点
int next1,next2;
for(int i = 0;i < 8;i++) {
next1 = th1 + delta[i];
next2 = th2 + delta[7 - i];
if(next1 > 8 || next1 < 1 || next2 > 8 || next2 < 1)
continue;
if(!visited[next1][next2]) {
Q.push(pair<int,int>(next1,next2));
visited[next1][next2] = true;
dis[next1][next2] = dis[th1][th2] + 1;
}
}
}

if(dis[v1][v2])
return dis[v1][v2];
else
return -1;
}

bool Do() {
char s1,v1;
int s2,v2;
if(scanf("%c%d %c%d\n",&s1,&s2,&v1,&v2) == EOF)
return false;
printf("To get from %c%d to %c%d takes %d knight moves.\n",
s1,s2,v1,v2,BFS(s1 - 'a' + 1,s2,v1 - 'a' + 1,v2));
return true;
}

int main() {
while(Do());
return 0;
}

发布评论
  • 点击查看/关闭被识别为广告的评论