hdu 1372 骑士从起点走到终点的步数 (BFS)
给出起点和终点 求骑士从起点走到终点所需要的步数
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.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int map[][];
int sx, sy;
int ex, ey;
bool flag;
char s1[] , s2[] ; int dx[] = {-,-,,,-,-,,} ;
int dy[] = {,,,,-,-,-,-} ; struct node
{
int x , y , step ;
}; int bfs()
{
queue<node> q ;
int i , fx ,fy ;
node now , t ;
now.x = sx ;
now.y = sy ;
now.step = ;
q.push(now) ;
memset(map , , sizeof(map)) ;
map[sx][sy] = ;
while(!q.empty())
{
now = q.front() ;
q.pop() ;
for (i = ; i < ; i++)
{
fx = now.x + dx[i] ;
fy = now.y + dy[i] ;
if (fx< || fy< || fx>= || fy>= || map[fx][fy] == )
continue ;
if (fx == ex && fy == ey)
{
return now.step+ ;
}
t.x = fx ;
t.y = fy ;
t.step = now.step+ ;
q.push(t) ;
map[fx][fy] = ;
}
}
return ;
} int main()
{
//freopen("in.txt","r",stdin) ;
while (scanf("%s %s" , &s1 , &s2) !=EOF)
{
sx = s1[] - 'a' ;
sy = s1[] - '' ;
ex = s2[] - 'a' ;
ey = s2[] - '' ;
printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs()) ;
} return ;
}
hdu 1372 骑士从起点走到终点的步数 (BFS)的更多相关文章
- # H - H HDU - 2066 (多起点、多终点问题)
H - H HDU - 2066 (多源点.多汇点问题) 一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到 ...
- hdu 1240 3维迷宫 求起点到终点的步数 (BFS)
题意,给出一个N,这是这个三空间的大小,然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐标和终点的坐标,输出到达的步数 比较坑 z是x,x是y,y是z,Sample InputSTART 1 ...
- hdu 1010 走到终点时刚好花掉所有时间 (DFS + 奇偶性剪枝 )
题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间.S为起点,D为终点.并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷.所以你必须每秒走一步,且到D点时,所用时间为T.用 ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- 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了两次---@_@) ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- HDU 1372
题意:模拟国际象棋马的走棋方式,和中国象棋一样马走日,8X8的棋盘,问从起点到终点的最短步数,国际象棋中数字代表行row,字母代表列column, 思路:记忆化深搜. #include<cstd ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- cdqz2017-test11-占卜的准备
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #defi ...
- LINQ to SQL 实现 CASE WHEN THEN 语句
Ø 前言 没有什么特别的,只是觉得 LINQ 的功能其实还是蛮强大的,所以简单记录下,算是工作笔记吧,有可能还能帮助到其他同学呢^_^. Ø 下面主要使用了 C# 三元运算符实现实现 SQL 中的 ...
- new和delete
和 sizeof 类似,sizeof不是函数,它是一个操作符,它在编译期就完成了计算,在函数运行期间它已经是一个常数值了. int a; sizeof(int) = 4; sizeof(a) = 4; ...
- The connection to adb is down, and a severe error has occured(Android模拟器端口被占用)
相信不少同学和我一样遇到这个问题,有时候搞的还要重启电脑,那究竟是什么原因导致的呢,很明显,你的端口被占用了,那下面给出终极解决方案 一.首先描述症状,如下图 二.出现问题了,首先确定你的sdk目录是 ...
- checklistboxx 多选取值 和选中
for (int i = 0; i < cklist.Items.Count; i++) { if (cklist.GetItemChecked(i)) { //修改子菜单的父节点为此菜单的id ...
- POJ1113 Wall【凸包】
题意: 求把城堡围起来需要的最小墙壁周长. 思路: 围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长. 代码: ...还是看大佬写的,自己做个记录方便日后复习 ...
- F - New Distinct Substrings (后缀数组)
题目链接:https://cn.vjudge.net/contest/283743#problem/F 题目大意:给你一个字符串,然后让你求出不同的子串的个数. 具体思路:首先,一个字符串中总的子串个 ...
- mysql 查询优化 ~ 善用profie利器
一 简介:利用profile分析慢语句的过程有助于我们进行语句的优化 二 执行过程 set profiling=1; set profiling=0; 2 执行sql 3 查看过程消耗 三 ...
- SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课
1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...
- systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。
1.centos 检查服务是否开机自启 (ntpd是原生的服务,mysql是注册的服务) 参考:1.http://man.linuxde.net/systemctl