Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27406    Accepted Submission(s): 9711

Problem Description
Angel
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.)

 
Input
First line contains two integers stand for N and M.

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.

 
Output
For
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."
 
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
 
Sample Output
13
 
Author
CHEN, Xue
 
Source
题意:
~
代码:
 //一道很简单的题卡了很长时间。从重点出发找起点,优先队列存
#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+优先队列的更多相关文章

  1. POJ 1724 ROADS(BFS+优先队列)

    题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...

  2. hdu 1242 找到朋友最短的时间 (BFS+优先队列)

    找到朋友的最短时间 Sample Input7 8#.#####. //#不能走 a起点 x守卫 r朋友#.a#..r. //r可能不止一个#..#x.....#..#.##...##...#.... ...

  3. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  4. hdu1839(二分+优先队列,bfs+优先队列与spfa的区别)

    题意:有n个点,标号为点1到点n,每条路有两个属性,一个是经过经过这条路要的时间,一个是这条可以承受的容量.现在给出n个点,m条边,时间t:需要求在时间t的范围内,从点1到点n可以承受的最大容量... ...

  5. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  6. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  7. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  8. hdu 2102 A计划 具体题解 (BFS+优先队列)

    题目链接:pid=2102">http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 開始看到四分之中的一个的AC率感 ...

  9. D. Lunar New Year and a Wander bfs+优先队列

    D. Lunar New Year and a Wander bfs+优先队列 题意 给出一个图,从1点开始走,每个点至少要经过一次(可以很多次),每次经过一个没有走过的点就把他加到走过点序列中,问最 ...

随机推荐

  1. LPC1768/1769之CAN控制器概述(附库函数下载地址)

    一.背景: 使用LPC1769来做CAN的收发,在此对使用LPC1769的CAN控制器进行收发做个总结和记录,以备下 次开发快速上手使用. 附:LPC1768/1769除了支持最高频率不同以外,其它基 ...

  2. 提高PHP代码质量的36个技巧

    1.不要使用相对路径 常常会看到: require_once('../../lib/some_class.php'); 该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录. 因此会 ...

  3. MySQL中find_in_set()和in的区别

    弄个测试表来说明两者的区别 CREATE TABLE `test` ( `id` int(8) NOT NULL auto_increment, `name` varchar(255) NOT NUL ...

  4. 【重点】Shell入门教程:流程控制(2)条件判断的写法

    第三节:条件判断的写法 if条件判断中,if的语法结构中的“条件判断”可以有多种形式.测试结果是真是假,就看其传回的值是否为0. 条件测试的写法,有以下10种: 1.执行某个命令的结果 这里的命令,可 ...

  5. PHP setcookie() 函数

    语法 setcookie(name,value,expire,path,domain,secure): name 必需.规定 cookie 的名称. value 必需.规定 cookie 的值. ex ...

  6. 【ASP.NET】利用Nuget打包package——GUI方式

    GUI方式 通过GUI的方式,可以下载如下的软件 NuGetPackageExplorer   打包dll 1.打开软件,在Package Content处点击右键 ,选择Add Lib 2.在lib ...

  7. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  8. AngularJS Bootstrap

    AngularJS 的首选样式表是 Bootstrap. 可以在 AngularJS 应用中加入 Twitter Bootstrap,你可以在你的 <head>元素中添加如下代码: < ...

  9. iOS LaunchImage 各尺寸

  10. 基于GMap.Net的地图解决方案

    一 地图的加载与显示 关于GMap的介绍与使用可以看我以前的文章: GMap.Net开发之在WinForm和WPF中使用GMap.Net地图插件 GMap.Net是.Net下一个地图控件,可以基于Ht ...