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. Angular JavaScript内存溢出问题 (FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory)

    方法一和方法二参考:https://www.cnblogs.com/liugang-vip/p/6857595.html 方法一:my-project/node_modules/.bin 下增大内存( ...

  2. vue在传值的时候经常遇到的问题

    在我用vue编写程序的时候,在传值的时候,经常会遇到些问题,像今天遇到了两个问题,在用父传子的方法去传值,当父组件中的要传的数据是for循环出来的或者是列表的时候,你想每次运行的事件,都去传某一行,或 ...

  3. Shell读取一个表达式并计算其结果

    #!/bin/bash # 读取一个算数表达式并计算出结果 # 如果输入 # 5+50*3/20 + (19*2)/7 # 则结果为 # 17.929 read x printf "%.3f ...

  4. 小程序之Button组件,函数的调用与简单的逻辑

    我们要实现一个简单的功能,在界面上放置一张图片,设置重新加载按钮,能更新图片. WXML代码: <!--index.wxml--> <view clas="index&qu ...

  5. spoj-TSUM Triple Sums

    题目描述 题解: 很吊的容斥+$FFT$,但是并不难. 首先,由于有重复,我们要容斥. 怎么办? 记录三个多项式, 只取一个:$w1$; 相同物体拿两个:$w2$; 相同物体拿三个:$w3$; 然后答 ...

  6. vue 项目规范

    1, 组件化 2, css 分清单独和通用的 3, 封装请求 4, 命名原则 1: 尽量和后端保持一致 2: 简单常见的单词 3: 全部小写

  7. Android-Intent and Intent Filters

    1.intent(意图)可以用来创建启动3种类型的基本情况:①To start an activity:启动一个活动②To start an service③To start an broadcast ...

  8. [MVC]Controller

    1,控制器中所有的动作方法必须声明为public,如声明为private或protected,将不被视为动作方法. 如果将Action声明为private,或者是添加[NonAction]属性,则不对 ...

  9. hadoop上传文件报错

    19/06/06 16:09:26 INFO hdfs.DFSClient: Exception in createBlockOutputStream java.io.IOException: Bad ...

  10. js总结(一):javascript的类型:基本类型、对象和数组

    javascript 类型分为2种,一个是原始值,另一个是复杂值(对象). 一.原始值 5个原始值是:数字,字符,布尔,null,undefined. 9个原生的对象构造函数:Number Strin ...