Uva 439 Knight Moves

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

Status

Description

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 !    
=============================================================================*/   
  
#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;
}


版权声明:欢迎在小站搜索磁力内容:bt书虫 www.btbook.top 千万级磁力存储,分布式爬虫。全国高校IT精英群号:467123855

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
  • kl28978113
  • 2014年08月10日 09:34
  • 1371

UVA 439 Knight Moves

题目:给出国际象棋棋盘中的两个点,求马从一个点跳到另一个点的最少步数。#include #include using namespace std; struct RA{ int c; ...
  • xf_zhen
  • xf_zhen
  • 2016年06月04日 14:48
  • 91

UVA——439 Knight Moves

题目: Knight Moves Problem Description A friend of you is doing research on the Traveling Knight ...
  • hunt_er
  • hunt_er
  • 2018年01月27日 20:38
  • 15

uva 439 Knight Moves

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

uva 439 Knight Moves

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

UVA - 439 Knight Moves

简单的题目,直接用BFS搜索,利用队列,计算出从开始点到每一个点的最短路径长度,最后输出到目标点的长度即可,具体实现见如下代码: #include #include #include #includ...
  • zju2016
  • zju2016
  • 2017年08月02日 08:49
  • 63

Uva - 439 - Knight Moves

、 bfs求最短路,经典数据结构题目。 AC代码: #include #include #include #include #include #include #include ...
  • zyq522376829
  • 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
  • littleorange6
  • 2015年10月10日 23:51
  • 184

Knight Moves UVA - 439

这题比较简单,对于我这种学弱学习bfs再好不过了。 下面是代码: /****************************************************************...
  • Mrhanice
  • 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
  • chengouxuan
  • 2012年01月10日 16:24
  • 835
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Uva 439 Knight Moves
举报原因:
原因补充:

(最多只允许输入30个字)