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. 简单版nginx lua 完成定向流量分发策略

    本文链接:https://www.cnblogs.com/zhenghongxin/p/9131362.html 公司业务前端是使用 “分发层+应用层” 双层nginx架构,目的是为了提高缓存的命中率 ...

  2. D - 统计同成绩学生人数

    点击打开链接 读入N名学生的成绩,将获得某一给定分数的学生人数输出.  Input 测试输入包含若干测试用例,每个测试用例的格式为  第1行:N  第2行:N名学生的成绩,相邻两数字用一个空格间隔.  ...

  3. bzoj4519: [Cqoi2016]不同的最小割(最小割树)

    传送门 好神仙……最小割树是个什么东西…… 其实我觉得干脆直接$O(n^2)$跑几个dinic算了…… 来说一下这个叫最小割树的神奇东西 我们先建一个$n$个点,没有边的无向图 在原图中任选两点$s, ...

  4. 伸展树的实现——c++

     一.介绍 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造.(01) 伸展树属于二叉 ...

  5. poj1220------高精度进制转换模板

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; const i ...

  6. 我与网站的日常-webshell命令执行

    本文比较基础,其中有一个知识点关于php执行系统命令的函数 ,我们用最简单的webshell来说说传值问题的影响, 本文作者: i春秋签约作家——屌丝绅士 0×01前言:    小表弟又来写文章了,这 ...

  7. day 50 AJAX 初入门

    前情提要: jq 学不好,ajax   难用好, 食用先请先确保最起码的jq 能会用 https://www.cnblogs.com/baili-luoyun/p/10473518.html  jq ...

  8. MiniUi-----Spinner 数值调节器(可以实现任意值的递增)

    Spinner 数值调节器可以实现任意值的递增,每次的递增值主要是通过increment="递增值"属性来控制的. 属性 该属性扩展自验证框(validatebox),下面是为微调 ...

  9. Window History对象

    History 对象属性 length 返回浏览器历史列表中的 URL 数量. History 对象方法 back() 加载 history 列表中的前一个 URL. window.history.b ...

  10. Odoo9.0模块开发全流程

    构建Odoo模块 模块组成 业务对象 业务对象声明为Python类, 由Odoo自己主动加载. 数据文件 XML或CSV文件格式, 在当中声明了元数据(视图或工作流).配置数据(模块參数).演示数据等 ...