Knight Moves

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 17

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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 <iostream>
#include<deque>
#include<cstring>
#include<cstdio> using namespace std;
struct node
{
int x,y,num;
};
deque<node> s;
int dr[][]={{,},{,-},{,},{,-},{-,-},{-,},{-,-},{-,} };
int vis[][];
int ans,i,sx,sy,tx,ty;
char ch1[],ch2[];
void bfs()
{
node p;
p.x=sx;
p.y=sy;
p.num=;
s.clear();
vis[sx][sy]=;
s.push_back(p);
while(!s.empty())
{
node q=s.front();
for(int i=;i<;i++)
{
int xx=q.x+dr[i][];
int yy=q.y+dr[i][];
if(xx> && xx<= && yy> && yy<= && !vis[xx][yy])
{
p.x=xx;
p.y=yy;
p.num=q.num+;
s.push_back(p);
vis[xx][yy]=;
if (xx==tx && yy==ty) {ans=p.num; return;}
}
}
s.pop_front();
}
return;
}
int main()
{
while(~scanf("%s %s",&ch1,&ch2))
{
ans=;
sx=ch1[]-'a'+;
sy=ch1[]-'';
tx=ch2[]-'a'+;
ty=ch2[]-'';
memset(vis,,sizeof(vis));
if (sx!=tx || sy!=ty) bfs();
printf("To get from %s to %s takes %d knight moves.\n",ch1,ch2,ans);
}
return ;
}

HDU1372:Knight Moves(BFS)的更多相关文章

  1. hdu1372 Knight Moves BFS 搜索

    简单BFS题目 主要是读懂题意 和中国的象棋中马的走法一样,走日字型,共八个方向 我最初wa在初始化上了....以后多注意... 代码: #include <iostream> #incl ...

  2. HDU1372:Knight Moves(经典BFS题)

    HDU1372:Knight Moves(BFS)   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %l ...

  3. HDU-1372 Knight Moves (BFS)

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

  4. HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏

    Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...

  5. poj2243 &amp;&amp; hdu1372 Knight Moves(BFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接: POJ:http: ...

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

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

  7. POJ 1915 Knight Moves(BFS+STL)

     Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20913   Accepted: 9702 ...

  8. UVA 439 Knight Moves(BFS)

    Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...

  9. HDU 1372 Knight Moves(BFS)

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

随机推荐

  1. 在64位Win7操作系统中安装Microsoft Access Engine的解决方案

    在64位Win7操作系统中安装Microsoft Access Engine的解决方案 现在的Win7系统中安装的一般都是32位的Office,因为微软推荐使用32位的Office,兼容性更强,稳定性 ...

  2. php练习1

    <?php$conn=mysqli_init();if(!conn){ echo "mysqli_init error"; exit(0);}$ret=mysqli_real ...

  3. redhat 安装hadoop1.2.1伪分布式

    完整安装过程参考:http://www.cnblogs.com/shishanyuan/p/4147580.html 一.环境准备    1.安装linux.jdk      2.下载hadoop2. ...

  4. Python基础学习笔记---5.输入\输出 I\O文件操作目录

    在很多时候,你会想要让你的程序与用户(可能是你自己)交互.你会从用户那里得到输入,然后打印一些结果.我们可以分别使用 raw_input 和 print 语句来完成这些功能.对于输出,你也可以使用多种 ...

  5. git容易被忽略的准备工作命令

    安装git命令 $ apt-get install git-core 配置用户信息 $ git config --global user.name "wxw" $ git conf ...

  6. ios导航栏又按钮添加图片后使其保持原色

    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyl ...

  7. iOS不可变字符串的所有操作

    可以直接复制代码即可运行看看结果,方便理解 //NSString //OC字符串不能用printf输出 //但是遗憾的是OC没有一个从终端读取数据的方式,需要使用scanf读取C字符串然后转换成OC的 ...

  8. python常用函数年初大总结

    1.常用内置函数:(不用import就可以直接使用) help(obj) 在线帮助, obj可是任何类型 callable(obj) 查看一个obj是不是可以像函数一样调用 repr(obj) 得到o ...

  9. Struts2原理图

  10. Python实战:爬虫的基础

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...