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. app/desktop/view/index.html 显示授权标识

    app/desktop/view/index.html 显示授权标识

  2. Bmob Androidstudio配置

    AndroidStudio配置 鉴于目前Google官方推荐使用 Android Studio 进行Android项目开发,自 V3.4.2 开始,Bmob Android SDK 可以使用Gradl ...

  3. js怎么判断浏览器类型

    <script type=“text/javascript”> function isIE(){return navigator.appName.indexOf(“Microsoft In ...

  4. stm32菜单按键的设计

    有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...

  5. js 总结累计大全

    1选择  select  获取val text   更改其他class值 <script type="text/javascript"> $(function(){ $ ...

  6. tomcat的几种配置方式(常用)

    https://www.baidu.com   url www.baidu.com 主机名 baidu.com 域名 第一种 放在webapp目录下 也可以放在ROOT 根目录下 访问路径 IP:端口 ...

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

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

  8. HDU 5800 To My Girlfriend

    背包变形.dp[i][j][g][h]表示前i个数字,和为j,有g个必选,有h个必不选的方案数. 答案为sum{dp[n][j][2][2]}*4 #pragma comment(linker, &q ...

  9. set集合容器

      set集合容器几条特点 1.它不会重复插入相同键值的元素,而采取忽略处理 2.使用中序遍历算法,检索效率高于vector.deque.list容器,在插入元素时,会自动将元素按键值从小到大排列 3 ...

  10. Python 学习笔记2

    今天继续安装配置python. Fear can hold you prisoner. Hope can set you free.