# 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

#include int x, y, max, dir[8][2] = {{2, 1}, {2, -1}, {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, {1, -2...
• kl28978113
• 2014年08月10日 09:34
• 1371

## UVA 439 Knight Moves

• xf_zhen
• 2016年06月04日 14:48
• 91

## UVA——439 Knight Moves

• hunt_er
• 2018年01月27日 20:38
• 15

## uva 439 Knight Moves

bfs+优先队列搜索最短路径 #include #include #include #include #include using namespace std; char buffer...
• xiaohaowudi
• 2013年10月09日 13:00
• 338

## uva 439 Knight Moves

uva 439 Knight Moves
• tengfei461807914
• 2016年08月25日 23:14
• 178

## UVA - 439 Knight Moves

• zju2016
• 2017年08月02日 08:49
• 63

## Uva - 439 - Knight Moves

、 bfs求最短路，经典数据结构题目。 AC代码： #include #include #include #include #include #include #include ...
• zyq522376829
• 2015年06月19日 09:26
• 312

## UVa 439 Knight Moves

#include #define N 8 int vis[N][N]; int step[N][N]; int dx[8] = {1,2,2,1,-1,-2,-2,-1}; int dy[8] = {...
• littleorange6
• 2015年10月10日 23:51
• 184

## Knight Moves UVA - 439

• Mrhanice
• 2017年03月16日 23:14
• 132

## uva 439 knight moves

BFS  Knight Moves  A friend of you is doing research on the Traveling ...
• chengouxuan
• 2012年01月10日 16:24
• 835

举报原因： 您举报文章：Uva 439 Knight Moves 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)