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. Linux系统初学-第一课 虚拟机安装CentOS6.5以及Root密码找回

    Linux系统初学第一课 虚拟机安装CentOS6.5以及Root密码找回 虚拟机安装CentOS6.5 一.安装虚拟机 1-1.安装虚拟机VMware Station,新建虚拟机,选择典型配置. 1 ...

  2. PHP导入Excel表

    初始化参数,先导入PHPExcel类 /** * 读出Excel表格数据 * @param $filename 文件名 * @param string $encode 编码格式 * @return a ...

  3. 项目-高性能TcpServer - 目录

    1.项目-高性能TcpServer - 1.网络通信协议 https://blog.csdn.net/arno1988/article/details/82463225 2.项目-高性能TcpServ ...

  4. Mysql Explain的简单使用

    Mysql Explain 主要重要的字段有上面红色方框圈出来的那几个. type: 连接类型,一个好的SQL语句至少要达到range级别,杜绝出现all级别. key: 使用到的索引名,如果没有选择 ...

  5. Python学习:17.Python面向对象(四、属性(特性),成员修饰符,类的特殊成员)

    一.属性(特性) 普通方法去执行的时候,后面需要加括号,特性方法执行的时候和静态字段一样不需要不需要加括号. 特性方法不和字段同名. 特性方法不能传参数. 在我们定义数据库字段类的时候,往往需要对其中 ...

  6. Leecode刷题之旅-C语言/python-231 2的幂

    /* * @lc app=leetcode.cn id=231 lang=c * * [231] 2的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  7. T脚本语言学习记录-工具(一)

    1.set & unset %set a Hello ;#定义变量 a 并赋值 =>Hello %puts $a ;#输出变量值 =>Hello %set a “Test Tcl” ...

  8. 10 Quality Free Flat Icon Sets for Your Designs

    Subscribe It’s clear that flat design has gained great popularity in recent years. This is hardly su ...

  9. 20155220 2016-2017-2 《Java程序设计》第八周学习总结

    20155220 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 第14章 NIO与NIO2 NIO简介 NIO使用频道来衔接数据结点,在处理数据时,NIO可 ...

  10. loj2538 「PKUWC 2018」Slay the Spire

    pkusc 快到了--做点题涨涨 rp. ref我好菜啊QAQ. 可以发现期望只是一个幌子.我们的目的是:对于所有随机的选择方法(一共 \(\binom{2n}{m}\)种),这些选择方法都最优地打出 ...