hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间
Sample Input
7 8
#.#####. //#不能走 a起点 x守卫 r朋友
#.a#..r. //r可能不止一个
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
bfs+优先队列
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std; int n, m;
char map[][];
int sx, sy;
bool flag; struct node
{
int x , y , step ;
bool operator <(const node &t) const
{
return step > t.step ;
}
}; int dx[] = {,,,-} ;
int dy[] = {,-,,} ; void bfs()
{
node now , t ;
int i , fx ,fy ;
priority_queue<node> q ;
now.x = sx ;
now.y = sy ;
now.step = ;
q.push(now) ;
map[sx][sy] = '#' ;
while(!q.empty())
{
now = q.top() ;
q.pop() ;
for (i = ; i < ; i++)
{
fx = now.x + dx[i] ;
fy = now.y + dy[i] ;
if (fx< || fy< || fx >=n || fy >=m ||map[fx][fy] == '#')
continue ;
if (map[fx][fy] == 'r')
{
printf("%d\n" , now.step+) ;
flag = ;
return ;
}
if (map[fx][fy] == 'x')
{
t.x = fx ;
t.y = fy ;
t.step = now.step + ;
q.push(t) ;
}
else
{
t.x = fx ;
t.y = fy ;
t.step = now.step + ;
q.push(t) ;
}
map[fx][fy] = '#' ; }
} } int main()
{
// freopen("in.txt","r",stdin) ;
while (scanf("%d %d" , &n , &m) !=EOF)
{
int i , j ;
for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j] ;
if (map[i][j] == 'a')
{
sx = i ;
sy = j ;
}
}
}
flag = ;
bfs() ;
if (!flag)
printf("Poor ANGEL has to stay in the prison all his life.\n") ;
} return ;
}
dfs
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; char map[][] ;
bool visit[][] ; int n , m ;
int MIN ; void dfs(int x , int y , int sum)
{
if (x< || y< || x>= n || y>= m)
return ;
if (map[x][y] == '#')
return ;
if (sum >= MIN)
return ;
if (visit[x][y] == )
return ;
if (map[x][y] == 'r')
{
if (sum < MIN)
MIN = sum ;
return ;
}
if (map[x][y] == 'x')
sum++ ;
visit[x][y] = ;
dfs(x+,y,sum+) ;
dfs(x-,y,sum+) ;
dfs(x,y+,sum+) ;
dfs(x,y-,sum+) ;
visit[x][y] = ;
} int main()
{
// freopen("in.txt","r",stdin) ; while (scanf("%d %d" , &n , &m) !=EOF)
{
if (n == && m == )
break ;
memset(visit , ,sizeof(visit)) ;
int i , j ;
int bx , by ;
int sum = ; for (i = ; i < n ; i++)
{
for (j = ; j < m ; j++)
{
cin>>map[i][j];
if (map[i][j] == 'a')
{
bx = i ;
by = j ; }
} } MIN = INT_MAX ;
dfs(bx,by,sum) ;
if (MIN != INT_MAX)
printf("%d\n" , MIN) ;
else
printf("Poor ANGEL has to stay in the prison all his life.\n") ; } return ;
}
hdu 1242 找到朋友最短的时间 (BFS+优先队列)的更多相关文章
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- HDU - 4198 Quick out of the Harbour (BFS+优先队列)
Description Captain Clearbeard decided to go to the harbour for a few days so his crew could inspect ...
- hdu 1548 A strange lift 宽搜bfs+优先队列
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 There is a strange lift.The lift can stop can at ...
- hdu 1026 Ignatius and the Princess I(BFS+优先队列)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1026 Ignatius and the Princess I Time Limit: 2000/100 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 1242 Rescue
题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...
- F - JDG HDU - 2112 (最短路)&& E - IGNB HDU - 1242 (dfs)
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- FBI阅人术——用最短的时间了解一个人
FBI阅人术--用最短的时间了解一个人 和陌生人第一次见面时,要如何在一开始谈话的几分钟内,了解这个人?如何和对方拉近距离?如何找到对方喜爱的话题?如何让对方愿意开口? 这都得依靠细心而入微的观察力, ...
随机推荐
- FZU - 1688 Binary land
题目链接 Problem 1688 Binary land Accept: 72 Submit: 171Time Limit: 1000 mSec Memory Limit : 3276 ...
- 在O(n) 时间复杂度,O(1)空间复杂度内反转单链表
在LeetCode中看到判断回文的程序:https://leetcode.com/problems/palindrome-linked-list/ 里面用单链表来存储数据,先反转前半部分的单链表,然后 ...
- MySql Workbench导出ER图并存为PDF文件
一.登陆数据库 二.点击Database => Reverse Engineer 三.填入登陆信息后next => next,选择要生成ER模型的数据库 四.点击next => n ...
- Nginx 虚拟主机示例
Nginx server 模块 server { // 标识虚拟主机开始 listen ; ## // 指定虚拟主机服务器端口 server_name localhost; // 指定 IP地址或者域 ...
- day2 查看文件目录命令:ls
查看当前文件夹下面多有的目录文件ls 查看当前目录下面所有的文件,包括隐藏的文件ls -a(或者两个一样ls -all) 显示除"."和".."外的所有文件ls ...
- 【黑客免杀攻防】读书笔记2 - 免杀与特征码、其他免杀技术、PE进阶介绍
第3章 免杀与特征码 这一章主要讲了一些操作过程.介绍了MyCCL脚本木马免杀的操作,对于定位特征码在FreeBuf也曾发表过类似工具. VirTest5.0特征码定位器 http://www.fre ...
- MySQL— 索引
目录 一.索引 二.索引类型 三.索引种类 四.操作索引 五.创建索引的时机 六.命中索引 七.其它注意事项 八.LIMIT分页 九.执行计划 十.慢查询日志 一.索引 MySQL索引的建立对于MyS ...
- Faster rcnn代码理解(3)
紧接着之前的博客,我们继续来看faster rcnn中的AnchorTargetLayer层: 该层定义在lib>rpn>中,见该层定义: 首先说一下这一层的目的是输出在特征图上所有点的a ...
- UML和模式应用5:细化阶段(9)---迈向对象设计
1.前言 开发者如何设计对象,可以采用如下三种方式: 编码:在编码的同时进行设计 绘图然后编码:绘制一些UML,然后转到如上编码方式,在集成开发环境中编码 只绘图,不编码:使用工具从图中生成一切 本章 ...
- camera驱动框架分析(上)【转】
转自:https://www.cnblogs.com/rongpmcu/p/7662738.html 前言 camera驱动框架涉及到的知识点比较多,特别是camera本身的接口就有很多,有些是直接连 ...