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. C++变量和基本类型

    1. 如何选择类型的准则 当明确知晓数值不可能为负的时候,应该选择无符号类型. 使用int执行整数运算的时候,在实际应用中,short常常显得太小而long一般和int有一样的尺寸,如果数值超过了in ...

  2. swift详解之十-------------异常处理、类型转换 ( Any and AnyObject )

    异常处理.类型转换 ( Any and AnyObject ) 1.错误处理 (异常处理) swift 提供第一类错误支持 ,包括在运行时抛出 ,捕获 , 传送和控制可回收错误.在swift中 ,错误 ...

  3. 洛谷P1421 小玉买文具

    这道题其实就是编程最基础的逻辑,没什么好讲的输入,输出就完了,非常简单! code: #include<cstdio> #include<iostream> using nam ...

  4. WCF未找到终结点

    配置都配了,仍然找不到,config文件没有重新加载,原因不详,只能重新编译一下,就好了....后续找找原因看看

  5. Java:清空文件内容

    文章来源:https://www.cnblogs.com/hello-tl/p/9139432.html import java.io.*; public class FileBasicOperati ...

  6. python中的多任务

    多任务 什么是任务 一个电脑运行这的软件 什么是多任务 电脑同时运行着的多个软件 多任务原理 时间片的轮转 并行与并发 并发:假的多任务,多个任务共用一个核 并行:正的多任务,一个核处理一个程序 生理 ...

  7. list、tuple、dict内部功能释义

    一.list内部功能释义 运用频次:☆☆☆☆☆ 1. append():列表末尾插入元素 [示例] >>> L1=[11,22,33] >>> L1.append( ...

  8. luoguT21777

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  9. win10 命令行无法直接ping的问题解决方法

    ping是在system32下的一个应用包里,设置完环境变量并保存之后若还是无法直接ping,可以考虑如下方法: 打开命令行窗口,键入以下命令: cd\ cd windows\system32 pin ...

  10. [vijos1159]岳麓山上打水

    [vijos1159]岳麓山上打水 试题描述 今天天气好晴朗,处处好风光,好风光!蝴蝶儿忙啊,蜜蜂也忙,信息组的同学们更加忙.最近,由于XX原因,大家不得不到岳麓山去提水.55555555~,好累啊. ...