Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14125    Accepted Submission(s): 8269

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



Source
 



Recommend
Eddy   |   We have carefully selected several similar problems for you:  1072 1240 1312 1241 1016 
 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
本题为典型的bfs深搜的模板题
本题只要了解bfs算法就能AC
但是要对国际象棋中的马的运动方式熟悉一下就没问题了
下面上我注释了的
谁都能看懂的代码
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
struct node
{
int x,y,step;
};
queue<node> q;
int dir[][]={{-,-},{-,-},{-,},{-,},{,},{,-},{,-},{,}};//马所能够跳的八个方向记录下来
int vis[][];
char temp[][];//定义一个字符串用于输入
int change(char c)//字符转数字
{
switch (c)
{
case 'a':return ;
break;
case 'b':return ;
break;
case 'c':return ;
break;
case 'd':return ;
break;
case 'e':return ;
break;
case 'f':return ;
break;
case 'g':return ;
break;
case 'h':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
case '':return ;
break;
}
}
int bfs(int x,int y,int x1,int y1)
{
while(!q.empty())//队列的初始化,全部清空
q.pop();
int i;
q.push(node{x,y,});
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x1&&t.y==y1)
return t.step;
for(i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<&&dy<&&!vis[dx][dy])//基本搜索
{
vis[dx][dy]=;
q.push(node{dx,dy,t.step+});
}
}
}
return ;
}
int main()
{
while(scanf("%s%s",temp[],temp[])!=EOF)
{
memset(vis,,sizeof(vis));
int x=change(temp[][]);
int y=change(temp[][]);
int x1=change(temp[][]);
int y1=change(temp[][]);//确定首尾点
int ans=bfs(x,y,x1,y1);
printf("To get from %s to %s takes %d knight moves.\n",temp[],temp[],ans);//输出
}
return ;
}

2018-11-16  00:03:31  Author:LanceYu

最新文章

  1. MFC-01-Chapter01:Hello,MFC---1.3 第一个MFC程序(02)
  2. 使用DOS比较两个txt文件的差异
  3. WIN7下USB多点触摸,一次发多个数据包的延迟问题,重要!
  4. android Editview中加小图标或者文字实现
  5. hdu5032 Always Cook Mushroom
  6. js计算时间差,包括计算,天,时,分,秒
  7. perl5 第六章 模式匹配
  8. JavaSE教程-02Java基本语法-思维导图
  9. layui数据表格以及传数据方式
  10. JS面向对象与面向过程
  11. 仿vue实现简易版mvvm双向绑定
  12. 手机QQ公众号亿级消息实时群发架构
  13. Lr-代理录制
  14. IDEA新建javaWeb项目
  15. ES系列一、CentOS7安装ES 6.3.1、集成IK分词器
  16. Python-HTML CSS 练习
  17. C++数组初始化方法
  18. Chrome插件(扩展)开发全攻略
  19. 修改机器名、IP对arcgis server、portal的影响?
  20. jquery 不选择第一个

热门文章

  1. RGB颜色查询
  2. ASP.NET开发实战——(二)为什么使用ASP.NET
  3. [TJOI2019]唱,跳,rap,篮球(生成函数,组合数学,NTT)
  4. Redis数据结构及常用命令(草稿)
  5. FWT-快速沃尔什变换
  6. 第23课 优先选用make系列函数
  7. canal
  8. Unity Shader 屏幕后效果——高斯模糊
  9. ArrayList 源码分析 基于jdk1.8:
  10. SQL忽略重复键作用