Rescue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 21510 Accepted Submission(s): 7671

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
Recommend

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
char MAP[202][202];
bool visit[202][202];
struct node{
int x,y;
int flag;
};
int u[4]={1,0,-1,0};
int v[4]={0,1,0,-1};
int cmp(node a,node b){
return a.flag<b.flag;
}
int BFS(int i,int j){
queue<node> Q; //Q要在数组内定义,否则要记得每次用之前将队列清空。由于上次的队列可能会由于没有全然出队而保留上次的数据。当中我在这就出了错! node t;
t.x=i; t.y=j;
t.flag=0;
Q.push(t);
visit[i][j]=1;
while(!Q.empty()){
t=Q.front();
int tx,ty;
tx=t.x;ty=t.y;
if(MAP[tx][ty]=='r'){
return t.flag;
}
Q.pop();
node temp[4];
int p=0,k;
for(k=0;k<4;++k){
if(MAP[tx+u[k]][ty+v[k]]!='#'&&!visit[tx+u[k]][ty+v[k]]){
if(MAP[tx+u[k]][ty+v[k]]=='x'){
temp[p].x=tx+u[k];
temp[p].y=ty+v[k];
visit[temp[p].x][temp[p].y]=1;
temp[p].flag=t.flag+2;
++p;
}
else{
temp[p].x=tx+u[k];
temp[p].y=ty+v[k];
visit[temp[p].x][temp[p].y]=1;
temp[p].flag=t.flag+1;
++p;
}
// Q.push(temp); //此处不能 直接入队,由于同等级的 r要比 x耗时短,所以先将 t上下左右的四个点存放到数组中
}
}
sort(temp,temp+p,cmp); //经过排列后再依次入队
for(k=0;k<p;++k){
Q.push(temp[k]);
}
}
return -1;
}
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
int i,j;
for(i=0;i<=n+1;++i){
for(j=0;j<=m+1;++j){
MAP[i][j]='#';
}
}
int ax,ay;
for(i=1;i<=n;++i){
getchar();
for(j=1;j<=m;++j){
scanf("%c",&MAP[i][j]);
if(MAP[i][j]=='a') ax=i,ay=j;
}
}
memset(visit,0,sizeof(visit));
int res=BFS(ax,ay);
if(res==-1) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",res);
}
return 0;
}

hdoj-1242-Rescue【广搜+优先队列】的更多相关文章

  1. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  2. hdoj 1242 Rescue

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. HDU 1242 Rescue (BFS+优先队列)

    题意:X代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙,走过.要花费一秒,走过x要花费2秒,求从起点到终点的最少时间. 析:一看到样例就知道是BFS了吧,很明显是最短路径问题,不过又加了一个条 ...

  4. HDU - 3345 War Chess 广搜+优先队列

    War chess is hh's favorite game: In this game, there is an N * M battle map, and every player has hi ...

  5. BFS HDOJ 1242 Rescue

    题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...

  6. 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 ...

  7. HDU 3152 Obstacle Course(优先队列,广搜)

    题目 用优先队列优化普通的广搜就可以过了. #include<stdio.h> #include<string.h> #include<algorithm> usi ...

  8. USACO Milk Routing /// 优先队列广搜

    题目大意: 在n个点 m条边的无向图中 需要运送X单位牛奶 每条边有隐患L和容量C 则这条边上花费时间为 L+X/C 求从点1到点n的最小花费 优先队列维护 L+X/C 最小 广搜到点n #inclu ...

  9. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

随机推荐

  1. 错误的语法:"create view必须是批处理中仅有的语句"

    编写脚本提示: 错误的语法:"create view必须是批处理中仅有的语句" FROM sys.views WHERE name = 'v_CS_UserRoleNames' ) ...

  2. nginx的配置和基本使用命令

    配置文件基本说明 配置文件位置:/usr/local/nginx/conf/nginx.conf #设置用户群,nobody代表低权限用户 #user nobody; #工作衍生进程数,通常代表CPU ...

  3. Django:调用css、image、js

    1.在项目的manage.py同级目录创建static.templates 2.编辑settings.py,在最后加入 STATIC_URL = '/static/' HERE = os.path.d ...

  4. 第二章:systemverilog声明的位置

    1.package 定义及从package中导入定义(***) verilog中,对于变量.线网.task.function的声明必须在module和endmodule之间.如果task被多个modu ...

  5. ARM Linux 3.x的设备树(Device Tree)(转)

    http://blog.csdn.net/21cnbao/article/details/8457546

  6. 跟http相关的

    http http 中     请求: 请求行    请求方式   采用协议   版本号     网址    请求头    客户端可以接受数据类型    可以接受语言     可以接受的压缩格式 请求 ...

  7. jQuery+ajax城市联动

    分享一下自己最近写的城市联动.技术使用ajax+jQuery实现. 首先请看前台的javascript代码. 以下是连个实现异步加载的方法. <script type="text/ja ...

  8. Leetcode 241.为运算表达式设计优先级

    为运算表达式设计优先级 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输 ...

  9. iis性能监控

    文章:对于IIS上的应用程序池监控 文章:IIS并发连接数及性能优化

  10. 用SQLLDR来装载date类型的控制文件

    以前给山东某单位做oracle数据库恢复得时候,恢复出来得数据中包含date类型,当时给客户提供得是sqlldr得方式,因为数据量比较大,用sqlldr装载起来速度比较快,所以采用了这种方式,结果在装 ...