E - Knight Moves

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

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.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<iostream> using namespace std; int startx,starty,endx,endy;
char a[3],c[3];
int b[100][100]; int dir[8][2]= {1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};//往八个方向搜 struct node
{
int x,y,step;
}; void bfs(int x,int y)
{
node st,ed;
memset(b,0,sizeof(b));
queue<node>q;
st.x=x;
st.y=y;
st.step=0;
b[st.x][st.y]=1;
q.push(st);
while(!q.empty())
{
st=q.front();
q.pop();
if(st.x==endx&&st.y==endy)
{
printf("To get from %s to %s takes %d knight moves.\n",a,c,st.step);
return;
}
for(int k=0; k<8; k++)
{
ed.x=st.x+dir[k][0];
ed.y=st.y+dir[k][1];
if(ed.x>=1&&ed.x<=8&&ed.y>=1&&ed.y<=8&&!b[ed.x][ed.y])
{
b[ed.x][ed.y]=1;
ed.step=st.step+1;
q.push(ed);
}
}
}
} int main()
{
while(~scanf("%s%s",a,c))
{
startx=a[0]-'a'+1;//把字母转化为数字模拟地图
starty=a[1]-'0';
endx=c[0]-'a'+1;
endy=c[1]-'0';
bfs(startx,starty);
}
return 0;
}


hdu 1372Knight Moves的更多相关文章

  1. hdu Knight Moves

    这道题实到bfs的题目,很简单,不过搜索的方向变成8个而已,对于不会下象棋的会有点晕. #include <iostream> #include <stdio.h> #incl ...

  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. HDU 1372 Knight Moves(bfs)

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

  4. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  5. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. HDU 1372 Knight Moves (bfs)

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

  7. HDU 1372 (搜索方向稍有改变) Knight Moves

    其实手写模拟一个队列也挺简单的,尤其是熟练以后. 尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马 所以搜索就有八个方向 对了注意初始化标记数组的时候,不要把起点标 ...

  8. HDU 1372 Knight Moves【BFS】

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

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

随机推荐

  1. 关于NuGet

    一.NuGet是什么? NuGet是Microsoft开发平台的程序集包管理器,它由客户端工具和服务端站点组成,客户端工具提供给用户管理和安装/卸载软件程序包,以及打包和发布程序包到NuGet服务端站 ...

  2. Web 开发者不可不知的15条编码原则

    HTML 已经走过了近20的发展历程.从HTML4到XHTML,再到最近十分火热的HTML5,它几乎见证了整个互联网的发展.但是,即便到现在,有很多基础的概念和原则依然需要开发者高度注意.下面,向大家 ...

  3. 下载Google My Tracks

    http://code.google.com/p/mytracks/source/browse/?name=2.0.2#hg%2FMyTracks%253Fstate%253Dclosed 需要类似的 ...

  4. Sublime之插件的安装(三)

    今天在写js的时候,突然想到一个问题就是能不能快速的对齐的插件,当当当~~~sublime这么神器当然有,那就是:Alignment插件 如果你写的代码是这样的: var a = , b =, ccc ...

  5. php常用函数——数学函数

    php常用函数——数学函数

  6. 安全测试===dos攻击和ddos攻击

    Dos攻击: dos攻击是Denial of Service的简称,即拒绝服务,造成DoS的攻击行为被称为DoS攻击,其目的是使计算机或网络无法提供正常的服务 DoS攻击是指故意的攻击网络协议实现的缺 ...

  7. 实现UE添加自定义按钮之添加菜单

    1.ueditor.config.js配置文件中配置 2.在ueditor.all.js配置文件中配置点开的的弹框位置 3.在ueditor1_4_3-utf8-jsp\themes\default\ ...

  8. ERROR in vc 6.0 (LINK : fatal error LNK1561: entry point must be defined)

    导致错误 LINK : fatal error LNK1561: entry point must be defined 的原因有很多种, 网上可以搜到很多, 一般是函数入口没定义, 或者修改为/su ...

  9. curl 发送请求的时候报错

    AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see ...

  10. pandas安装及使用

    一. 安装pandas1. Anaconda        安装pandas.Python和SciPy最简单的方式是用Anaconda.Anaconda是关于Python数据分析和科学计算的分发包.2 ...