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

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

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.

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std;
int vis[9][9];
int ans=999999999;
int sx,sy;
int ex,ey;
int dis[8][2]= {-1,-2,-2,-1,-2,1,-1,2,1,2,2,1,2,-1,1,-2};
struct node {
int x,y;
int step;
};
int check(int x,int y) {
if(x<1||y<1||x>8||y>8)
return 1;
if(vis[x][y])
return 1;
return 0;
}
void bfs() {
node a,next;
a.x=sx;
a.y=sy;
a.step=0;
queue<node> q;
q.push(a);
vis[sx][sy]=1;
while(!q.empty()) {
a=q.front();
q.pop();
if(a.x==ex&&a.y==ey) {
if(ans>a.step)
ans=a.step;
return;
}
for(int i=0; i<8; i++) {
next.x=a.x+dis[i][0];
next.y=a.y+dis[i][1];
if(check(next.x,next.y))
continue;
next.step=a.step+1;
vis[next.x][next.y]=1;
q.push(next);
}
}
}
int main() {
// freopen("input.txt","r",stdin);
char ch1[5],ch2[5];
while(scanf("%s %s",ch1,ch2)!=EOF) {
ans=999999999;
memset(vis,0,sizeof(vis));
sx=ch1[0]-'a'+1;
// cout<<sx<<endl;
sy=ch1[1]-'1'+1;
ex=ch2[0]-'a'+1;
ey=ch2[1]-'1'+1;
bfs();
printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
}
return 0;
}

hdu 1372 BFS的更多相关文章

  1. HDU<1372>/bfs

    题目连接 简单bfs搜索 #include <set> #include <map> #include <cmath> #include <queue> ...

  2. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  3. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  5. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  6. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  7. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

  8. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  9. (step4.2.1) hdu 1372(Knight Moves——BFS)

    解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...

随机推荐

  1. go-ethereum源码分析 PartIII 共识流程

    A: js指令转化为新transaction 在web3上操作流程 1. import to web3 2. connect to peers 3. read local key store 4. d ...

  2. Android使用Jenkins自动化构建测试打包apk

    Jenkins这东西搭建起来真是一点也不省心啊,看着别人的教程摸着石头过河,配置的东西有点多啊,稍有不慎,就构建不成功啦!即使步骤跟别人一样也会报各种乱七八糟的错误啊哈哈~~这东西只能佛系搭建~~在经 ...

  3. Android Studio之SVN打分支、切换分支及合并分支

    1.打分支: 右击项目--Subversion--Branch or Tag 点击OK,分支就创建成功了,接下来我们切换到分支v2 2.切换分支: 右击项目--Subversion--Update D ...

  4. python运算符与数据类型

    python运算符 Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员运算符 身份运算符 运算符优先级 以下假设变量: a=10,b=20: ...

  5. java读取各种类型文件

    用到的几个包 bcmail-jdk14-132.jar/bcprov-jdk14-132.jar/checkstyle-all-4.2.jar/FontBox-0.1.0-dev.jar/lucene ...

  6. python pexpect包的一些用法

    转自:https://www.jianshu.com/p/cfd163200d12 mark一下,原文中写的挺详细

  7. 并查集(POJ1182)

    链接:http://poj.org/problem?id=1182 定义一种关系R(x,y),x > y 时 R(x,y) = 2:x = y 时 R(x,y)= 1:x < y 时 R( ...

  8. css的position

    1.标准流2.浮动3.定位块级元素:div.H1-H6.有序及无序列表(ol.ul.li).p内联元素:a.span.img 1. 介绍 1.1 说明 Position 属性:规定元素的定位类型.即元 ...

  9. 使用Spark进行搜狗日志分析实例——列出搜索不同关键词超过10个的用户及其搜索的关键词

    package sogolog import org.apache.hadoop.io.{LongWritable, Text} import org.apache.hadoop.mapred.Tex ...

  10. Scrapy中间件user-agent和ip代理使用

    一.定义实现随机User-Agent的下载中间件 1.在middlewares.py中完善代码 import random from Tencent.settings import USER_AGEN ...