Problem Description

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.

Source

University of Ulm Local Contest 1996
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int visit[][];
int x1,y1,x2,y2;
int dir[][]={{-,-},{-,},{-,},{-,-},
{,-},{,},{,},{,-}};
struct node
{
int x,y;
int step;
};
int BFS(int step)
{
if(x1==x2&&y1==y2)
return step;
int i,x,y,s;
queue<node> q;
node top,temp;
top.x=x1,top.y=y1,top.step=;
q.push(top);
while(!q.empty())
{
top=q.front();
q.pop();
for(i=;i<;i++)
{
x=top.x+dir[i][];
y=top.y+dir[i][];
s=top.step;
if(x>&&y>&&x<=&&y<=&&visit[x][y]==)
{
visit[x][y]=;
++s;
if(x==x2&&y==y2)
return s;
temp.x=x;
temp.y=y;
temp.step=s;
q.push(temp);
}
}
}
return -;
}
int main()
{
char str1[],str2[],t;
while(~scanf("%s%s",str1,str2))
{
memset(visit,,sizeof(visit));
x1=str1[]-'a'+;
y1=str1[]-'';
x2=str2[]-'a'+;
y2=str2[]-'';
visit[x1][y1]=;
t=BFS();
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,t);
}
return ;
}
 

HDU-1372 Knight Moves (BFS)的更多相关文章

  1. HDU 1372 Knight Moves(bfs)

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

  2. HDU 1372 Knight Moves (bfs)

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

  3. 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 ...

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

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

  5. HDU 1372 Knight Moves【BFS】

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

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

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

  7. HDU 1372 Knight Moves (广搜)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  8. 杭州电 1372 Knight Moves(全站搜索模板称号)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

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

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

  10. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

随机推荐

  1. Java实战之02Hibernate-01简介、常用接口、CRUD操作

    一.Hibernate简介 1.Hibernate在开发中所处的位置 2.ORM映射 Object :面向对象领域的 Relational:关系数据库领域的 Mapping:映射 Object: Re ...

  2. Codevs 3729 飞扬的小鸟

    飞扬的小鸟 标签 动态规划 NOIp提高组 2014 难度 提高+/省选- 题目描述 Flappy Bird 是一款风靡一时的休闲手机游戏.玩家需要不断控制点击手机屏幕的频率来调节小鸟的飞行高度,让小 ...

  3. HTML5 声明兼容IE的写法

    <!DOCTYPE html> <!–[if IE]> <meta http-equiv=”X-UA-Compatible” content=”IE=8″ /> & ...

  4. 高效的VS调试技巧

    本文总结了十个调试技巧,当你使用VS的时候可以节省你很多时间. 1.悬停鼠标查看表达式 调试有时候很有挑战性,当你步入一个函数想看看哪块出错的时候,查看调用栈来想想值是从哪来的.另一些情况下,则需要添 ...

  5. docker无法连接进程

    simon@simon-X550LD:~$ docker run hello-world docker: Cannot connect to the Docker daemon. Is the doc ...

  6. Python的面向对象4

    今天我们接着来聊聊继承! 那什么是继承呢? 新写的类是不必重新编写,只要从现有的类继承,就自动拥有了该类的所有功能,新类只需要编写现有类缺少的功能,可以复用已有的代码! python的继承的特点: 继 ...

  7. Mvvm Light Toolkit for WPF/Silverlight系列之搭建mvvmlight开发框架

    Mvvm Light Toolkit for WPF/Silverlight系列之搭建mvvmlight开发框架   本章节,我将通过示例介绍如何搭建mvvmlight开发环境.示例中的我会针对wpf ...

  8. 基于u-boot源码的简单shell软件实现

    一.概述 1.shell概念 Shell(命令解析器),它用于接收用户输入的命令,进行解析,然后调用相应的应用程序,为使用者提供了使用软件的界面. shell是操作系统最外面的一层.shell管理你与 ...

  9. 配置mybatis错误总结

    ### The error may exist in SQL Mapper Configuration ### Cause: org.apache.ibatis.builder.BuilderExce ...

  10. chmod 命令 set uid ,set gid,sticky bit 说明

    permission的符号模式表: 模式 名字 说明 r 读 设置为可读权限 w 写 设置为可写权限 x 执行权限 设置为可执行权限 X 特殊执行权限 只有当文件为目录文件,或者其他类型的用户有可执行 ...