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

HDU 1372 Knight Moves 题解的更多相关文章

  1. HDU 1372 Knight Moves(最简单也是最经典的bfs)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDU 1372 Knight Moves(BFS)

    题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...

  3. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  4. HDU 1372 Knight Moves

    最近在学习广搜  这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...

  5. [宽度优先搜索] HDU 1372 Knight Moves

    Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. HDU 1372 Knight Moves (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  8. ZOJ 1091 (HDU 1372) Knight Moves(BFS)

    Knight Moves Time Limit: 2 Seconds      Memory Limit: 65536 KB A friend of you is doing research on ...

  9. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

随机推荐

  1. 在springboot中使用redis缓存,将缓存序列化为json格式的数据

    背景 在springboot中使用redis缓存结合spring缓存注解,当缓存成功后使用gui界面查看redis中的数据 原因 springboot缓存默认的序列化是jdk提供的 Serializa ...

  2. 1.Vue前端核心分析

    1.Vue SoC:关注点分离原则 网络通信:axios 页面跳转:vue-router 页面管理:vuex Vue-UI:ICE.ElementUI 集大成者:MVVM+虚拟DOM 2.MVVM 异 ...

  3. VeeValidate——vue2.0表单验证插件

    一.vee-validate入门 vee-validate 是一个轻量级的 vue表单验证插件.它有很多开箱即用的验证规则,也支持自定义验证规则.它是基于模板的,因此它与HTML5验证API类似且熟悉 ...

  4. Linux性能优化实战学习笔记:第三讲

    一.关于上下文切换的几个为什么 1. 上下文切换是什么? 上下文切换是对任务当前运行状态的暂存和恢复 2. CPU为什么要进行上下文切换? 当多个进程竞争CPU的时候,CPU为了保证每个进程能公平被调 ...

  5. [LeetCode] 892. Surface Area of 3D Shapes 三维物体的表面积

    On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[i][j] represents a tower of v cu ...

  6. AntDeploy发布前端项目到IIS(脱离vs单独使用)

    AntDeploy AntDeploy是一款开源的一键发布部署工具,目的是代替重复性的发布动作,提高部署效率 1.一键部署iis 2.一键部署windows服务 3.一键部署到Docker 4.支持增 ...

  7. 转 A 、B两张表,找出ID字段中,存在A表,但是不存在B表的数据

    A.B两张表,找出ID字段中,存在A表,但是不存在B表的数据,A表总共13W数据,去重后大约3万条数据,B表有2W条数据,且B表的ID有索引. 方法一 使用not in,容易理解,效率低. selec ...

  8. python3 字符和数字(ASC码)转换

    print(ord('b')) print(ord('B')) print(chr(98)) print(chr(66)) 结果:98 66 b B 也可以数字转ASC码,原理一样,如下(结果就不输出 ...

  9. Destoon手机搜索点击提示 http 403 forbidden解决方法

    以下是网上搜到的答案: 最近发现用destoon开发的手机版网站,在手机版百度搜素网站的时候,点击之后出现 http 403 forbidden的弹出窗.必须再次的刷新网页才可以打开网站.出现这个问题 ...

  10. Ext.net SelectionModel RowSelection

    <SelectionModel> <ext:RowSelectionModel ID="RowSelectionModel1308" runat="se ...