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 ;
}

最新文章

  1. SqlServerException:拒绝对表对象的select,insert权限解决(新建账号导致的问题)
  2. MSSQL 判断一个时间段是否在另一个时间段内!
  3. spring 4.2.0后jdbcTemplate中不用queryForLong了(之系统升级发现)
  4. Qt编写自定义控件二动画按钮
  5. 一种透明效果的view
  6. java-冒泡排序
  7. mac book air 装win7
  8. Java Tomcat 中调用.net DLL的方法
  9. Streams Studio配置Build options
  10. Maven + Jetty + Jersey搭建RESTful服务
  11. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
  12. ubuntu下如何安装和卸载wine-qq
  13. php注册登录源代码
  14. 使用Homebrew安装MySQL
  15. 利用百度地图api实现定位
  16. 学习笔记《Java多线程编程实战指南》三
  17. centos7设置默认的内核启动
  18. linux echo设置颜色
  19. Access数据库审计工具mdbtools
  20. 【BZOJ1922】[Sdoi2010]大陆争霸 Dijkstra

热门文章

  1. ThinkPHP微信扫码支付接口
  2. MySQL高级函数case的使用技巧----与sum结合实现分段统计
  3. linux 学习第十一天
  4. jQuery增减类操作代码
  5. ajaxSubmit请求返回数据成功,但是不执行success回调函数
  6. PhpStorm 全局查找的快捷键
  7. PHP读取excel表格,和导出表格
  8. php wamp基础环境搭建
  9. hash环/consistent hashing一致性哈希算法
  10. ruby学习笔记(3)- 新手入门