HDU1242 BFS+优先队列
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27406 Accepted Submission(s): 9711
was caught by the MOLIGPY! He was put in prison by Moligpy. The prison
is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs,
and GUARDs in the prison.
Angel's friends want to save Angel.
Their task is: approach Angel. We assume that "approach Angel" is to get
to the position where Angel stays. When there's a guard in the grid, we
must kill him (or her?) to move into the grid. We assume that we moving
up, down, right, left takes us 1 unit time, and killing a guard takes 1
unit time, too. And we are strong enough to kill all the guards.
You
have to calculate the minimal time to approach Angel. (We can move only
UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of
course.)
Then
N lines follows, every line has M characters. "." stands for road, "a"
stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
each test case, your program should output a single integer, standing
for the minimal time needed. If such a number does no exist, you should
output a line containing "Poor ANGEL has to stay in the prison all his
life."
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
//一道很简单的题卡了很长时间。从重点出发找起点,优先队列存
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
int n,m;
struct Lu
{
int x,y,cnt;
friend bool operator<(Lu a,Lu b)
{
return a.cnt>b.cnt;
}
};
bool vis[][];
int dir[][]={,,-,,,,,-};
char ch[][];
int bfs(int sx,int sy)
{
priority_queue<Lu>q;
Lu L1,L2;
L1.x=sx;L1.y=sy;L1.cnt=;
q.push(L1);
vis[sx][sy]=;
while(!q.empty())
{
L2=q.top();
q.pop();
if(ch[L2.x][L2.y]=='r')
return L2.cnt;
for(int i=;i<;i++)
{
L1.x=L2.x+dir[i][];
L1.y=L2.y+dir[i][];
if(L1.x<||L1.x>=n||L1.y<||L1.y>=m) continue;
if(vis[L1.x][L1.y]) continue;
if(ch[L1.x][L1.y]=='#') continue;
if(ch[L1.x][L1.y]=='x')
L1.cnt=L2.cnt+;
else L1.cnt=L2.cnt+;
vis[L1.x][L1.y]=;
q.push(L1);
}
}
return -;
}
int main()
{
int sx,sy;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++)
{
scanf("%s",ch[i]);
for(int j=;j<m;j++)
{
if(ch[i][j]=='a')
{
sx=i;sy=j;
}
}
}
memset(vis,,sizeof(vis));
int ans=bfs(sx,sy);
if(ans==-) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",ans);
}
return ;
}
HDU1242 BFS+优先队列的更多相关文章
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
- hdu 1242 找到朋友最短的时间 (BFS+优先队列)
找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...
- HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)
题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...
- hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)
题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...
- BFS+优先队列+状态压缩DP+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others) Memo ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- hdu 2102 A计划 具体题解 (BFS+优先队列)
题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...
- D. Lunar New Year and a Wander bfs+优先队列
D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...
随机推荐
- JavaScript事件处理程序
JavaScript中的事件处理程序主要分为3种: HTML事件处理程序: <script type="text/javascript"> // HTML事件处理程序 ...
- Ubuntu 14 中,SecureCRT、SecureFX个性化设置
[SecureCRT 个性化设置] 打开设置路径:菜单栏 -> Opions -> Global Options -> General -> Default Session - ...
- PHP用curl伪造IP和来源
以前没有这么搞过. 今天群里一个朋友在问这个问题. 查了下,CURL确实很强悍的可以伪造IP和来源. 1.php 请求 2.php . 1.php代码: $ch = curl_init(); curl ...
- PHP文件上传至另一台服务器
PHP程序上传文件时,想指定上传到另一台服务器. move_uploaded_file这个方法第二个参数指定的存放路径需要和php程序文件一个服务器,没办法指定其他服务器的地址,本人才疏学浅,哪位大哥 ...
- PHP定时备份MySQL,mysqldump语法大全
几个常用操作: 1.备份 # 只导出表结构 d:/PHP/xampp/mysql/bin/mysqldump -h127.0.0.1 -P3306 -uroot -p123456 snsgou_sns ...
- mongodb更新操作
除了查询条件,还可以使用修改器对文档进行更新. 1. $inc > db.tianyc03.find() { "_id" : ObjectId("50ea6b6f1 ...
- MySQL备份之【mydumper 学习】
MySQL在备份方面包含了自身的mysqldump工具,但其只支持单线程工作,这就使得它无法迅速的备份数据.而mydumper作为一个实用工具,能够良好支持多线程工作,这使得它在处理速度方面十倍于传统 ...
- python之函数
本节主要内容: 1. 上节遗留的内置函数: callable(), chr(),ord(), compile(), eval(),exec(), divmod(), isinstance() ,fil ...
- linux 汇编
nasm的语法和大学教材上8086的汇编伪指令有些差别,指令都是一样的. 编辑器就是普通的编辑器,vim,emacs,gedit,kate源文件类型为ascii码的plain text 编译用gcc或 ...
- 《C#高级编程(第六版)》泛型学习笔记(一):泛型优点和特性 (转载)
原文出处:http://www.cnblogs.com/xun126/archive/2011/01/13/1933838.html 泛型是CLR 2.0的一个新特性,在CLR 1.0中,要创建一个灵 ...