给出起点和终点 求骑士从起点走到终点所需要的步数

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)的更多相关文章

  1. # H - H HDU - 2066 (多起点、多终点问题)

    H - H HDU - 2066 (多源点.多汇点问题) 一个图上,有M条边,Z个出发点,Y个终止点.求一条最短路,其中起点是Z中的任意一点,终点是Y中任意一点. Input 输入数据有多组,输入直到 ...

  2. hdu 1240 3维迷宫 求起点到终点的步数 (BFS)

    题意,给出一个N,这是这个三空间的大小,然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐标和终点的坐标,输出到达的步数 比较坑 z是x,x是y,y是z,Sample InputSTART 1 ...

  3. hdu 1010 走到终点时刚好花掉所有时间 (DFS + 奇偶性剪枝 )

    题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间.S为起点,D为终点.并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷.所以你必须每秒走一步,且到D点时,所用时间为T.用 ...

  4. HDU 1372 Knight Moves(bfs)

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

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

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

  6. HDU 1372 Knight Moves【BFS】

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

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

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

  8. HDU 1372

    题意:模拟国际象棋马的走棋方式,和中国象棋一样马走日,8X8的棋盘,问从起点到终点的最短步数,国际象棋中数字代表行row,字母代表列column, 思路:记忆化深搜. #include<cstd ...

  9. HDU 1372 Knight Moves

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

随机推荐

  1. POJ - 1019 Number Sequence (思维)

    https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...

  2. UESTC - 1324 卿学姐与公主

    题目链接 某日,百无聊赖的卿学姐打开了某11区的某魔幻游戏 在这个魔幻的游戏里,生活着一个美丽的公主,但现在公主被关押在了魔王的城堡中. 英勇的卿学姐拔出利刃冲向了拯救公主的道路. 走过了荒野,翻越了 ...

  3. 01、@ConfigurationProperties 将属性文件里的值映射到JavaBean

    @ConfigurationProperties("person") //或是prefix属性 @Component //必须注册成容器中的bean被容器管理起来 public c ...

  4. JavaScript之小工具之日志log()[兼容]

    function log(){ try{ console.log.apply(console,arguments); }catch(e){ try{ opera.postError.apply(ope ...

  5. 2018-2019-2 网络对抗技术 20165227 Exp4 恶意代码分析

    2018-2019-2 网络对抗技术 20165227 Exp4 恶意代码分析 实验步骤: 使用的设备:Win7(虚拟机).kali(虚拟机) 实验一:使用如计划任务,每隔一分钟记录自己的电脑有哪些程 ...

  6. swift学习第一天---常量变量基础数据类型

    import Foundation /** * 1.常量 变量 知识要点:常量的定义用let 变量的定义用var 常量一旦定义便不可再更改. 变量定义之后可以在定义之后的程序中任意地方进行修改. */ ...

  7. Informatic学习总结_day01

    1.forlder 必须open之后才出现  mapping的界面 2.Source Qualifer 3.小技巧 验证自己写的转换格式是否正确 提前过滤一些数据,informatica工具的本质也是 ...

  8. ROS 多台计算机联网控制机器人

    0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...

  9. 记一次Win Server 2012部署问题及解决方法

    1.前言 本章内容为在win server 2012服务器部署时遇到的问题及解决方法.大致工作为:两台服务器,一台web.一台数据库:web服务器部署.net web程序,数据库服务器安装oracle ...

  10. 【逆向工具】IDA使用1-VS2015版本debug查找Main函数,加载符号文件

    IDA 常见操作 空格,切换反汇编视图 选择CALL或是跳转 进入函数内部或是跳转处 返回键 ESC daq.exe 分析32位程序 ,生成的IDA数据库文件是 .idb Idap64.exe 分析6 ...