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.
 
题目意思:给出骑士的初始位置和最后的目标位置,问骑士最少需要多少步就能到达目标。
 
解题思路:我们首先需要明确的是这是一个8*8的矩阵,列用字母a-h表示,行用字母1-8表示,最重要的是明白国际象棋中的骑士也就是马,也是走的‘'日'字。
准确说走的是3*2的格子中的对角线。

 中国象棋的棋子是摆在横线与竖线的交点上,而国际象棋却是将棋子摆在格子内,明白这一点也就直到为什么骑士也是走‘’日字的了,有图可知骑士最多能够向八个方位移动,那么我们只需进行一下八向的BFS即可。
 
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
char s1[],s2[];
struct node
{
int x;
int y;
int step;
};
int vis[][];
int to[][]= {{-,},{-,},{,},{,},{,-},{,-},{-,-},{-,-}};
int ex,ey;
int judge(int x,int y)
{
if(x>=&&y>=&&x<&&y<&&!vis[x][y])
{
return ;
}
else
{
return ;
}
}
int BFS()
{
int i;
queue<node>q;
node p,a;
p.x=s1[]-'a';
p.y=s1[]-'';
p.step=;
ex=s2[]-'a';
ey=s2[]-'';
vis[p.x][p.y]=;
q.push(p);
while(!q.empty())
{
a=q.front();
if(a.x==ex&&a.y==ey)
{
return a.step;
}
for(i=;i<;i++)
{
node b;
b=a;
b.x+=to[i][];
b.y+=to[i][];
if(judge(b.x,b.y))
{
b.step++;
vis[b.x][b.y]=;
q.push(b);
}
}
q.pop();
}
}
int main()
{
while(scanf("%s%s",s1,s2)!=EOF)
{
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,BFS());
memset(vis,,sizeof(vis));
memset(s1,sizeof(s1),);
memset(s2,sizeof(s2),);
}
return ;
}

Knight Moves(广搜BFS)的更多相关文章

  1. UVA 439 Knight Moves --DFS or BFS

    简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...

  2. poj3126 Prime Path 广搜bfs

    题目: The ministers of the cabinet were quite upset by the message from the Chief of Security stating ...

  3. hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  5. 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS

    图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...

  6. luoguP4112 [HEOI2015]最短不公共子串 SAM,序列自动机,广搜BFS

    luoguP4112 [HEOI2015]最短不公共子串 链接 luogu loj 思路 子串可以用后缀自动机,子序列可以用序列自动机. 序列自动机是啥,就是能访问到所有子序列的自动机. 每个点记录下 ...

  7. Knight Moves (双向bfs)

    # 10028. 「一本通 1.4 例 3」Knight Moves [题目描述] 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数.骑士一步可以移动到的位置由下图给出. [算法 ...

  8. POJ 3126 Prime Path 简单广搜(BFS)

    题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...

  9. HDU 5336——XYZ and Drops——————【广搜BFS】

    XYZ and Drops Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

随机推荐

  1. win 10 如何关闭自动更新

    1.右键“此电脑”图标,点击“管理”,打开“计算机管理”窗口; 2.在“计算机管理”窗口中找到“服务和应用程序”,点击“服务”,打开“服务”窗口; 3.在“服务”中找到“Windows Update” ...

  2. Hadoop源码学习笔记之NameNode启动场景流程五:磁盘空间检查及安全模式检查

    本篇内容关注NameNode启动之前,active状态和standby状态的一些后台服务及准备工作,即源码里的CommonServices.主要包括磁盘空间检查. 可用资源检查.安全模式等.依然分为三 ...

  3. mkdir 的详细使用说明

    mkdir 是make directory [dɪˈrɛktəri, daɪ-]的缩写,directory--目录的意思 mkdir在linux中是指新建文件目录 例如:mkdir test3 如果要 ...

  4. STM32使用FatFs

    1.定义一些变量在我们代码开始的部分,先定义一些变量供我们使用.这里选择几个来解析一下.第一个FIL file;这个变量是文件的结构体变量,记录了我们打开的文件的信息.使用f_open等函数的时候都要 ...

  5. sqli-labs (less-8-less-10)

    盲注需要掌握一些MySQL的相关函数:length(str):返回str字符串的长度.substr(str, pos, len):将str从pos位置开始截取len长度的字符进行返回.注意这里的pos ...

  6. 什么是PHP7中的孤儿进程与僵尸进程

    什么是PHP7中的孤儿进程与僵尸进程 基本概念 我们知道在unix/linux中,正常情况下,子进程是通过父进程创建的,子进程在创建新的进程.子进程的结束和父进程的运行是一个异步过程,即父进程永远无法 ...

  7. STM32F103C8T6、STM32F103ZET6工程模板

    STM32F103C8T6工程模板,推荐使用以下最新版本 最终版 2018 7 16  https://pan.baidu.com/s/1lIdZ2awus_quVu332RvJ6Q https:// ...

  8. 【翻译】理解 LSTM 网络

    目录 理解 LSTM 网络 递归神经网络 长期依赖性问题 LSTM 网络 LSTM 的核心想法 逐步解析 LSTM 的流程 长短期记忆的变种 结论 鸣谢 本文翻译自 Christopher Olah ...

  9. Pytorch之Variable求导机制

    自动求导机制是pytorch中非常重要的性质,免去了手动计算导数,为构建模型节省了时间.下面介绍自动求导机制的基本用法. #自动求导机制 import torch from torch.autogra ...

  10. 分享daocloud联合创始人陈齐彦关于docker的一段阐述

    罗比,本名陈齐彦,他在加入DaoCloud之前是EMC中国研究院的总架构师,云平台及应用实验室的创始人.谈及创业的初心,他激动了起来: 容器这东西和当年Hadoop一样,是互联网技术对企业IT技术的逆 ...