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. java第十次笔记

  2. bluemix创建docker容器

    简介: bluemix是基于kubernetes来服务的免费云空间.绑定信用卡后可以创建一个月的集群,一个月后会被删除. 下面示例介绍如何使用kubernetes dashboard来创建一个容器,并 ...

  3. 简单的Java ee思维导图

  4. Python随笔--爬虫(下载妹子图片)

  5. Redis操作1

    本文章内容节选自<PHP MVC开发实战>一书第16.4.2章节. 一.概述 Redis是一个NoSQL数据库,由于其数据类型的差异,所以要在MVC框架中实现CURD操作,比较繁锁.事实上 ...

  6. 练习 map集合被使用是因为具备映射关系 "进度班" "01" "张三" "进度班" "02" "李四" "J1701" "01" "王五" "J1701" "02" "王二" 此信息中,我们要怎样把上述信息装入集合中, 根据班级信息的到所有的所有信

    package com.rf.xs; import java.util.Arrays; public class Student01 { String name; int age; public St ...

  7. oracle入门之分页查询

    oracle的分页查询共三种方法 1.根据ROWID来分页(速率一般) SQL>select * from emp where rowid in (select rid from (select ...

  8. java多线程中的调度策略

    两种线程的调度模式: 抢占式调度: 抢占式调度指的是每条线程执行的时间.线程的切换都由系统控制,系统控制指的是在系统某种运行机制下,可能每条线程都分同样的执行时间片,也可能是某些线程执行的时间片较长, ...

  9. pytorch预训练

    Pytorch预训练模型以及修改 pytorch中自带几种常用的深度学习网络预训练模型,torchvision.models包中包含alexnet.densenet.inception.resnet. ...

  10. 1. 做node项目 (第二个月)

    工作栈: Node + Express + Mongoose +  Mongodb + Vuejs 主要做了 mongodb的 curd , 因为以前做 PHP + MySql 所以基本大同小异. n ...