Rescue

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

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
/*
广搜
检查了很久 最后 被困死的时候是 0,
if(visit[i][j]<num && visit[i][j]!=0)
考虑了一些情况,题意很清晰,有多个r。一个a么?应该是。但是我没有处理。
由于x的存在使得 到达各点的时间可能存在多样,也加进去比较了。
但是之前写的dfs,没有考虑过这样的情况。
*/
#include<stdio.h>
#include<stdlib.h>
#define HH 11111111
char a[][];
int map[][]={{,},{,},{,-},{-,}};
int zhan[],len;
int visit[][];
int n,m;
void bfs(int x,int y)
{
int i,x1,y1;
zhan[++len]=x;
zhan[++len]=y;
visit[x][y]=;
while(len>)
{
y=zhan[len--];
x=zhan[len--];
for(i=;i<;i++)
{
x1=x+map[i][];
y1=y+map[i][];
if(x1>=&&x1<=n && y1>=&&y1<=m)
{
if(visit[x1][y1]== && a[x1][y1]!='#')
{
if(a[x1][y1]=='.'||a[x1][y1]=='r')
visit[x1][y1]=visit[x][y]+;
else if(a[x1][y1]=='x')
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
if(visit[x1][y1]> && a[x1][y1]!='#')
{
if((a[x1][y1]=='.'||a[x1][y1]=='r')&&visit[x1][y1]>visit[x][y]+)
{
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
if(a[x1][y1]=='x' && visit[x1][y1]>visit[x][y]+)
{
visit[x1][y1]=visit[x][y]+;
zhan[++len]=x1;
zhan[++len]=y1;
}
}
}
}
}
}
int main()
{
int i,j,num;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%s",a[i]+);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
visit[i][j]=;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='a')
{
len=;
bfs(i,j);
}
}
num=HH;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
if(a[i][j]=='r')
{
if(visit[i][j]<num && visit[i][j]!=)
num=visit[i][j];
}
if(num==HH) printf("Poor ANGEL has to stay in the prison all his life.\n");
else printf("%d\n",num);
}
return ;
}

单纯的广搜,在浙大oj超时.... 蒋神却过了,思想很厉害。

/*
优先队列
*/ #include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<queue>
#define HH 11111111
using namespace std;
char a[][];
int visit[][];
int n,m;
int map[][]={{,},{,},{-,},{,-}};
struct node
{
friend bool operator< (node n1,node n2)
{
return n1.p>n2.p;
}
int p;
int x;
int y;
};
void bfs(int x,int y)
{
int i,x1,y1;
priority_queue<node>b;
while(!b.empty())
{
b.pop();
}
node tmp,tmp1;
tmp.x=x;
tmp.y=y;
tmp.p=;
b.push(tmp);
visit[x][y]=;
while(b.size()>)
{
tmp=b.top();
b.pop();
for(i=;i<;i++)
{
x1=tmp.x+map[i][];
y1=tmp.y+map[i][];
if(x1>=&&x1<=n && y1>=&&y1<=m && visit[x1][y1]== && a[x1][y1]!='#')
{
if(a[x1][y1]=='x')
visit[x1][y1]=tmp.p+;
else if(a[x1][y1]=='.' || a[x1][y1]=='r')
visit[x1][y1]=tmp.p+;
tmp1=tmp;
tmp.x=x1;
tmp.y=y1;
tmp.p=visit[x1][y1];
b.push(tmp);
tmp=tmp1;
if(a[x1][y1]=='r')return;
}
}
}
}
int main()
{
int i,j,num;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%s",a[i]+);
memset(visit,,sizeof(visit));
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='a')
{
bfs(i,j);
}
}
num=HH;
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
if(a[i][j]=='r' && visit[i][j]!= && visit[i][j]<num)
num=visit[i][j];
}
if(num==HH)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",num);
}
return ;
}

hdu Rescue 1242的更多相关文章

  1. hdu Rescue (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242 简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz 代码: #include < ...

  2. hdu Rescue

    因为要求的是最少的时间,很明显的是一个利用优先队列的bfs的题目,题目很一般. #include"iostream" #include"algorithm" # ...

  3. hdu 1242 Rescue

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

  4. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  5. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  6. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

  7. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

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

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

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

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

随机推荐

  1. xgboost 和GBDT的区别

    作者:wepon链接:https://www.zhihu.com/question/41354392/answer/98658997来源:知乎 传统GBDT以CART作为基分类器,xgboost还支持 ...

  2. Math-645. Set Mismatch

    The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...

  3. “借刀杀人”之CSRF拿下盗图狗后台

    最近我一个做贸易的朋友找到我,他发现自己拍摄的图片又被某个同行盗用了,而且是全站的图片基本都被盗用. 之前对方是引用他的图片链接,后面我给他做了防盗链解决了,现在对方是先下载图片,然后自己上传到服务器 ...

  4. (samba启动失败)smb.service: main process exited, code=exited, status=1/FAILURE

    按照指示,前往:journalctl -xe 没什么发现,搜的时候有人说也可以查看 journalctl -r 打出来之后我也看不出什么门道来 又看到有人说 smb 方面可以看看 testparm 我 ...

  5. hexo安装总结

    博客原文地址:Claiyre的个人博客 如需转载,请在文章开头注明原文地址 hexo真心是一个不错的东西呢,安装简单(然而对博主来说并不是这样,伤心脸),主题样式简洁优雅,还有多种选择. 流程如下 安 ...

  6. 数据库中"DDL","DML","DCL"

    sql组成:DDL:数据库模式定义语言,关键字:createDML:数据操纵语言,关键字:Insert.delete.updateDCL:数据库控制语言 ,关键字:grant.removeDQL:数据 ...

  7. 汽车检测SIFT+BOW+SVM

    项目来源于 <opencv 3计算机视觉 python语言实现> 整个执行过程如下: 1)获取一个训练数据集. 2)创建BOW训练器并获得视觉词汇. 3)采用词汇训练SVM. 4)尝试对测 ...

  8. 54.Storm环境搭建

    集群环境搭建 关闭防火墙,修改/etc/hosts配置(3台机器的ip可以相互通信) 下载安装jdk7(1.6以上),配置JAVA_HOME, CLASSPATH 搭建Zookeeper集群(保证3台 ...

  9. 安装nginx和nginx-gridfs和mongodb

    1.安装依赖包: [root@mongo_rs1 ~]# yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++ [roo ...

  10. Centos7下CPU内存等资源监控

    1.查看内存使用情况: [root@takeout web-takeout]# free -m total used free shared buff/cache available Mem: 378 ...