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) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
随机推荐
- 我的日志分析之道:简单的Web日志分析脚本
前言 长话短说,事情的起因是这样的,由于工作原因需要分析网站日志,服务器是windows,iis日志,在网上找了找,github找了找,居然没找到,看来只有自己动手丰衣足食. 那么分析方法我大致可分为 ...
- 10-SQL Server 2008 R2安装步骤
一. 软件和环境 1. 软件 : SQL Server 2008 R2 企业版 软件下载地址:XXXX 2. 环境要求: .Net FrameWork 3.5 以上 (windows 7 ...
- asp.net mvc Htmlhelper简单扩展
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Managers经理/代理形式的数据共享
代理方式实现进程间共享字典和列表形式的数据, 代码块: import os from multiprocessing import Process,Manager def f(d,l,normalLi ...
- HashMap原理分析(JDK1.7.x之前)
HashMap 实现Map.Cloneable.Serializable接口,继承AbstractMap基类. HashMap map = new HashMap<String,String&g ...
- mysql 案例 ~ pt-xtrabackup 使用
一 简介:学习innobackup工具使用 二 功能: 备份全库/单库/单表 三 常用命令 一 全库 目的 每天日常备份 备份 innobackupex --defaults-file=/etc ...
- Activity生命周期函数、onSaveInstanceState()和onRestoreInstanceState()的介绍
http://www.cnblogs.com/tianzhijiexian/p/3885472.html
- 实用技能之Python打包制作成EXE可执行程序
制作环境:Andconda3,python3.6 一.安装pyInstaller 方式一): 在命令行输入:pip install pyinstaller 方式二): ① 下载pyInstalle ...
- centOS6.4 extundelete工具恢复rm -rf 删除的目录[转]
原文:http://www.cnblogs.com/patf/p/3368765.html PS:补充下,我在fedora 19上运行的时候遇到的一个问题: 1 [root@localhost ext ...
- Sql Server 2008 数据库18456错误怎么解决?
可以windows连接,以前都可以,昨天突然就不可以用SQL连接,报18456错误. 1.以windows验证模式进入数据库管理器. 2.右击sa,选择属性: 在常规选项卡中,重新填写密码和确认密码( ...