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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- 小程序Promise不支持finally解决方案
小程序Promise不支持finally解决方案 代码片段 点击链接即可在微信开发者工具中查看代码wechatide://minicode/t2eidemj7P3X git地址 基本思路 小程序的Pr ...
- jquery 兼容的滚轮事件
// jquery 兼容的滚轮事件 $(document).on("mousewheel DOMMouseScroll", function (e) { ? : -)) || // ...
- jQuery-easyui和validate表单验证实例
jQuery EasyUI 表单 - 表单验证插件validatebox 使用时需要向页面引入两个css文件如下: <link rel="stylesheet" href=& ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
- C# 部分类使用partial修饰
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace pati ...
- android AsyncHttpClient使用
1.www.github.com下载jar包 loopj/android-async-http 将下载好的文件导入项目中 2.main.xml <?xml version="1.0&q ...
- saltstack系列~第一篇
一 简介:从今天开始学习saltstack 二 salt的认证系列操作 1 原理 saltstack通过/etc/salt/pki/目录下面的配置文件的密钥进行通信,master端接受minion端后 ...
- SpringMVC参数绑定(四)
1.默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. HttpServletRequest 通过request对象获取请求信息 HttpServletResponse ...
- GCC的符号可见性——解决多个库同名符号冲突问题
引用自:https://github.com/wwbmmm/blog/wiki/gcc_visibility 问题 最近项目遇到一些问题,场景如下 主程序依赖了两个库libA的funcA函数和libB ...
- 64位Win7系统WMware安装Mac OS
1. 准备工作 l VMWare Workstation,我的版本是 l MAC OS安装光盘镜像文件,种子地址 http://www.kuaipan.cn/file/id_611 ...