Uva 439 Knight Moves

原创 2015年11月18日 19:55:08
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu



Download as PDF

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 : 哈尔滨理工大学    
#      Last modified: 2015-11-18 19:56  
#     Filename: H.cpp   
#     Description:    
#        The people who are crazy enough to think they can change the world, are the ones who do !    
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));
  q.push(Node(s1, t1, 0));
  vis[s1][t1] = 1;
    Node u = q.front();
    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';
        printf("To get from %s to %s takes %d knight moves.\n",cnt1, cnt2, res);
    return 0;

版权声明:强烈建议大家加入(Java)全国各校IT精英! 群号 467123855 里面都是大神各种分享 内推渠道 (我的老豆比IT站被阿里销毁了。。。) 举报


UVa 439-Knight Moves


UVA - 439 Knight Moves

Knight Moves  A friend of you is doing research on the Traveling Knight Problem (TKP) w...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

UVA 439 Knight Moves (BFS)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=380 ...

UVA 439 Knight Moves

题目骑士移动分析bfs.代码#include #include #define N 9int map[N][N]; struct point { int x, y, s; } que[N*...

UVA 439 Knight Moves 走象棋 (DFS or BFS)

【题目链接】click here~~ 【题目大意】类型于中国象棋里面“马”的走法,给你两个坐标,一个初始坐标,一个最终坐标,在保证有解的情况下最小的步数 【思路】BFS的话,直接模拟,因为棋盘比较小 ...

uva 439 Knight Moves

Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where you are t...

UVA 439 Knight Moves(BFS)

题目大意: 给你起点和终点,按照象棋里面的象走日的走法,要走几步。 解题思路: 这代题目调试了几个小时,原来是错在节点坐标没初始化,所以做题还是要小心啊。

UVa439 习题 6-4 骑士的移动(Knight Moves)

原题链接: UVa-439 题目大意:  骑士在一个8*8的棋盘上移动,1-8代表行号,a-h代表列号,给出骑士的初始位置和目的位置,求骑士最少的移动步数.刘汝佳的字数上这题给了骑士移动的规律的图形...

UVa439---Knight Moves(BFS应用)

题目描述: A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find ...

UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves【BFS】

问题链接:UVA439 POJ2243 HDU1372 ZOJ1091 Knight Moves。基础级练习题,用C++语言编写程序。 题意简述:给出国际象棋棋盘中的两个点,求马从一个点跳到另一个点...