舒哥的blog

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

Uva 439 Knight Moves

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


阅读更多
版权声明:www.6aiq.com AIQ-机器学习大数据技术社区 全国最专业的机器学习大数据技术社区,机器学习与人工智能AI的前沿阵地,提供各种国内外第一手的机器学习、人工智能资源资讯学习资料,是一个以机器学习,大数据,云计算,数据分析,数据挖掘,人工智能,区块链为主要学习方向的学习交流社区。大家可以分享高质量文章 。全国高校IT精英群号:467123855 https://blog.csdn.net/lsgqjh/article/details/49912163
文章标签: 编程 bfs uva 哈理工
个人分类: dfs bfs 搜索
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭