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. flink源码学习

    传送门: https://www.jianshu.com/c/c9b356caf8a6

  2. vijos2055 移动金币

    题目链接 思路 首先这是一个阶梯博弈. 我们将金币两两组合,如果对方移动前一个,那么我们把后一个移动相同的距离,局面相当于没有变化.如果对方移动后一个,就相当于\(NIM\)游戏中,取走了一些石子. ...

  3. nginx小知识

    What Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 反向代理 当我们在外网访问百度的时候,其实会进行一个转发,代理到内 ...

  4. openldap 指定普通用户登录ldap后可查看某分组下的用户信息

    #ldap普通用户登录限制查看信息#在/openldap/slapd.conf文件最下面添加一下代码,可控制某个用户拥有查看用户信息的权限,而其他普通用户登录后无法查看用户信息,若有多个普通用户需要用 ...

  5. Notepad++显示内容自动换行

  6. 修咻咻对追光的人、云打印团队的Beta产品测试报告

    修咻咻对追光的人.云打印团队的Beta产品测试报告 作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平 ...

  7. linux重定向 null和zero

    文件描述符 linux下一切皆文件 文件描述符,是内核为了高效管理已经被打开的文件所创建的索引,用于指向被打开的文件,所有执行I/O操作的系统调用都通过文件描述符; 文件描述符是一个简单的非负整数,用 ...

  8. 系统内置委托Action和func

    Action委托, action是系统内置的委托,它可指向无返回值,没有参数的方法. using System; using System.Collections.Generic; using Sys ...

  9. postgresql NUMERIC(precision, scale)

  10. zbar android sdk在CentOS 7下的编译和使用

    环境:CentOS 7+NDK 20.0.5594570+libiconv-1.14 下载 下载libiconv-1.14版本,地址: https://ftp.gnu.org/pub/gnu/libi ...