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.

InputThe 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. 
OutputFor 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个方向,,直接用BFS查找就可以了

#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
struct stu{
int a,b;
};
char a,b;
int aa,bb;
int arr[][];
int arr1[][];
int arr2[][];
int ar[][]={{-,-},{-,},{,},{,-},{,},{,-},{-,},{-,-}};
int bfs(int x1,int y1,int x2,int y2){
memset(arr1,,sizeof(arr1));
queue<stu>que;
que.push({x1,y1});
arr1[x1][y1]=;
arr2[x1][y1]=;
while(que.size()){
int x=que.front().a;
int y=que.front().b;
que.pop();
int dx,dy;
for(int i=;i<;i++)
{
dx=x+ar[i][];
dy=y+ar[i][];
if(dx>=&&dx<&&dy>=&&dy<&&arr1[dx][dy]!=){
arr1[dx][dy]=;
arr2[dx][dy]=arr2[x][y]+;
que.push({dx,dy});
if(dx==x2&&dy==y2){
return arr2[dx][dy];
}
}
}
}
return -;
}
int main()
{
while(~scanf("%c%d %c%d",&a,&aa,&b,&bb)){
getchar();
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
arr[i][j]=;
}
}
int x1=a-'a';
// cout<<x1<<endl;
int y1=aa-;
// cout<<y1<<endl;
int x2=b-'a';
// cout<<x2<<endl;
int y2=bb-;
// cout<<y2<<endl;
arr[x1][aa-]=;
arr[x2][bb-]=;
if(x1==x2&&y1==y2)
printf("To get from %c%d to %c%d takes 0 knight moves.\n",a,aa,b,bb);
else {
int y=bfs(x1,y1,x2,y2);
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,aa,b,bb,y);
}
} return ;
}

最新文章

  1. [FromBody]与[FromUrl]
  2. 【Android学习】Windows下Android环境搭建
  3. mysql 不是主键不能删除的保护问题解决办法?
  4. IOS RSA 加密方式
  5. mysql学习链接
  6. java运算
  7. golang之pkg(包)
  8. HEAP CORRUPTION DETECTED
  9. C++零食:使用Unicode版的预定义宏__FUNCTION__
  10. java 解析excel
  11. Ext表格分页
  12. Java的数组,栈,队列
  13. 解决layui table方法渲染时时间格式问题
  14. HDU 1522 Marriage is Stable 【稳定婚姻匹配】(模板题)
  15. logback中logger详解
  16. loj6062 pair
  17. pandas分组group
  18. 【BZOJ2244】[SDOI2011]拦截导弹(CDQ分治)
  19. Structured Streaming教程(3) —— 与Kafka的集成
  20. shell字符串基本操作

热门文章

  1. hdu2203kmp匹配
  2. http服务部署
  3. 2017-12-08高级.net 面试小结
  4. [模拟] Codeforces - 1191C - Tokitsukaze and Discard Items
  5. Mysql性能优化:什么是索引下推?
  6. 写给小白看的入门级 Java 基本语法,强烈推荐
  7. ​结合异步模型,再次总结Netty多线程编码最佳实践
  8. NKOJ 【NOIP2015 Day2】运输计划
  9. [转载]Docker容器无法被stop or kill问题
  10. Win32程序:与&quot;LPCWSTR&quot;类型的形参不兼容