HDU 1372 Knight Moves 题解
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14125 Accepted Submission(s): 8269
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.
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
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.
//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 题解的更多相关文章
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- 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 ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
随机推荐
- flink源码学习
传送门: https://www.jianshu.com/c/c9b356caf8a6
- vijos2055 移动金币
题目链接 思路 首先这是一个阶梯博弈. 我们将金币两两组合,如果对方移动前一个,那么我们把后一个移动相同的距离,局面相当于没有变化.如果对方移动后一个,就相当于\(NIM\)游戏中,取走了一些石子. ...
- nginx小知识
What Nginx是一款轻量级的Web服务器.反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用. 反向代理 当我们在外网访问百度的时候,其实会进行一个转发,代理到内 ...
- openldap 指定普通用户登录ldap后可查看某分组下的用户信息
#ldap普通用户登录限制查看信息#在/openldap/slapd.conf文件最下面添加一下代码,可控制某个用户拥有查看用户信息的权限,而其他普通用户登录后无法查看用户信息,若有多个普通用户需要用 ...
- Notepad++显示内容自动换行
- 修咻咻对追光的人、云打印团队的Beta产品测试报告
修咻咻对追光的人.云打印团队的Beta产品测试报告 作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平 ...
- linux重定向 null和zero
文件描述符 linux下一切皆文件 文件描述符,是内核为了高效管理已经被打开的文件所创建的索引,用于指向被打开的文件,所有执行I/O操作的系统调用都通过文件描述符; 文件描述符是一个简单的非负整数,用 ...
- 系统内置委托Action和func
Action委托, action是系统内置的委托,它可指向无返回值,没有参数的方法. using System; using System.Collections.Generic; using Sys ...
- postgresql NUMERIC(precision, scale)
- zbar android sdk在CentOS 7下的编译和使用
环境:CentOS 7+NDK 20.0.5594570+libiconv-1.14 下载 下载libiconv-1.14版本,地址: https://ftp.gnu.org/pub/gnu/libi ...