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. Django(出版社功能)

    day62 day62 2018-05-02  1. 内容回顾     Django         1. 安装             1. Django版本 1.11.xx            ...

  2. Python面向对象(成员修饰符)

    day25 成员修饰符 class Foo: def __init__(self, name, age): self.name = name self.__age = age#私有,外部无法直接访问 ...

  3. mybatis源码追踪1——Mapper方法用法解析

    Mapper中的方法执行时会构造为org.apache.ibatis.binding.MapperMethod$MethodSignature对象,从该类源码中可以了解如何使用Mapper方法. [支 ...

  4. 程序猿的日常——SpringMVC系统架构与流程回顾

    web开发经历了很漫长的时间,在国内也快有十几年的时间了.从最开始的进程级到现在的MVC经历了很多的改进和优化,本篇就主要复习了解下Spring MVC相关的知识. 发展历程 第一阶段 CGI进程响应 ...

  5. Android逆向-Android基础逆向7(内购干货集合)

    本文作者:MSTLab-EvilChen 0×00 前言 首先,本来想写NDK的,但是还是先把这个流程过一遍吧,这个流程是必不可少的.其次,RMB真的是一个好东西. 导航 由于本人为了节省时间,不想贴 ...

  6. 聊聊jvm系列

    http://blog.csdn.net/column/details/talk-about-jvm.html

  7. 编写线程安全的Java缓存读写机制 (原创)

    一种习以为常的缓存写法: IF value in cached THEN return value from cache ELSE compute value save value in cache ...

  8. 为什么程序员老在改 Bug,就不能一次改好吗?

    程序员的日常三件事:写Bug.改Bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含Bug. 但是真的有这么多Bug要改吗?就不能一次改完吗? 程序员听这问题后要拍键盘了,还!真! ...

  9. 用汇编语言(ARM 32位)编写TCP Bind Shell的菜鸟教程

    用汇编语言(ARM 32位)编写TCP Bind Shell的菜鸟教程 来源 https://www.4hou.com/info/news/9959.html Change 新闻 2018年1月19日 ...

  10. IIS 8 配置错误

    1) ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed Th ...