## 舒哥的blog

www.6aiq.com AIQ-机器学习大数据技术社区 全国最专业的机器学习大数据技术社区。全国IT精英群号:467123855...

# Uva 439 Knight Moves

 Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

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 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

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.

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#
#     Filename: H.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int cnt[10][10];
int dir[][2] = {{2, -1}, {2, 1}, {1, 2}, {-1, 2}, {-2, 1},{-2, -1}, {-1, -2}, {1, -2}};
int vis[10][10];

int s1, t1, s2, t2;
int res ;
struct Node{
int x, y, step;
Node(int x, int y, int z):x(x), y(y), step(z){}
};

bool check(int x, int y){
if(x >= 0  && x < 8 && y >= 0 && y < 8 && !vis[x][y]){
return true;
}
return false;
}
void bfs(){
memset(vis, 0, sizeof(vis));
queue<Node>q;
q.push(Node(s1, t1, 0));
vis[s1][t1] = 1;
while(!q.empty()){
Node u = q.front();
q.pop();
if(u.x == s2 && u.y == t2){
res = u.step;return ;
}
for(int i = 0; i < 8; i++){
int tx = u.x + dir[i][0];
int ty = u.y + dir[i][1];
if(check(tx, ty)){
q.push(Node(tx, ty, u.step + 1));
vis[tx][ty] = 1;
}
}
}
}
int main()
{
char cnt1[10];
char cnt2[10];
while(scanf("%s", cnt1) != EOF){
res = 0;
s1 = cnt1[0] - 'a';
t1 = 8 - cnt1[1] + '0';
scanf("%s", cnt2);
s2 = cnt2[0] - 'a';
t2 = 8 - cnt2[1] + '0';
bfs();
printf("To get from %s to %s takes %d knight moves.\n",cnt1, cnt2, res);
}
return 0;
}


#### UVa 439 - Knight Moves

2013-10-26 05:31:02

#### uva439 - Knight Moves

2015-02-09 23:34:33

#### uva 439 Knight Moves 骑士移动

2015-07-22 14:23:21

#### uva 439 knight moves

2012-01-10 16:24:04

#### UVA 439 Knight Moves

2015-07-31 21:23:52

#### Uva - 439 - Knight Moves

2015-06-19 09:26:14

#### Uva 439 Knight Moves

2016-04-05 18:58:38

#### UVA - 439 Knight Moves

2014-08-24 08:32:26

#### UVA 439 Knight Moves

2016-07-20 20:04:09

#### UVa 439 Knight Moves

2015-10-10 23:51:32