Rescue

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

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



在BFS的搜索过程中,不能一判断出到达目标位置就退出BFS过程,否则求出来的仅仅只是r到a的最小步数。因为BFS所搜索的顶点都是按深度进行搜索的,所以BFS先搜索到的都是步数最少的,不一定是最优解,所用的时间可能更长。一定要等到链表为空,BFS搜索过程全部结束才能得出最优解或者得出无法找到目标位置的结论。
这一题并没有判断位置是否访问过,但是并不会无限循环下去。因为从某个位置出发判断是否要将它相邻的位置(x,y)入列,条件是这种走法比以前走到(x,y)所用的时间更少;
如果所用的时间更少,则(x,y)位置会重复入列,但不会无限下去。



 #include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
#define MAX 1000000
int Map[][];
int T[][];
int dir[][]= {{,},{-,},{,},{,-}};
int n,m;
int si,sj,di,dj;
int sign=;
typedef struct pointer
{
int x,y;
int time;
struct pointer *next;
} LNode,*LinkList;
LinkList ptr;
void bfs(LinkList head);
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
scanf("%c",&Map[i][j]);
T[i][j]=MAX;
if(Map[i][j]=='a')
{
di=i;
dj=j;
}
else if(Map[i][j]=='r')
{
si=i;
sj=j;
T[si][sj]=;
}
}
getchar();
}
LinkList p;
p=(LinkList)malloc(sizeof(LNode));
p->x=si;
p->y=sj;
p->time=;
p->next=NULL;
sign=;
bfs(p);
if(T[di][dj]<MAX)cout<<T[di][dj]<<endl;
else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
return ;
}
void bfs(LinkList head)
{
int i,fx,fy;
while(head!=NULL)
{
for(i=; i<; i++)
{
fx=head->x+dir[i][];
fy=head->y+dir[i][];
if(fx>=&&fx<n&&fy>=&&fy<m&&Map[fx][fy]!='#')
{
LinkList p;
if(sign==) ptr=head;
p=(LinkList)malloc(sizeof(LNode));
p->x=fx;
p->y=fy;
p->time=head->time+;
if(Map[fx][fy]=='x') p->time++;
if(p->time<T[fx][fy])
{
T[fx][fy]=p->time;
ptr->next=p;
p->next=NULL;
ptr=ptr->next;
sign=;
}
}
}
head=head->next;
}
}

HDOJ1242 Rescue(营救) 搜索的更多相关文章

  1. HDU 1242 Rescue 营救天使

    Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...

  2. ZH奶酪:【数据结构与算法】搜索之BFS

    1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...

  3. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  4. 搜索专题: HDU1242 Rescue

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)

    Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...

  6. Win7启动修复(Ubuntu删除后进入grub rescue的情况)

    起因:装了win7,然后在另一个分区里装了Ubuntu.后来格掉了Ubuntu所在的分区.系统启动后出现命令窗口:grub rescue:_ 正确的解决方式: 1.光驱插入win7安装盘或者用USB启 ...

  7. hdu----(4308)Saving Princess claire_(搜索)

    Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  8. Win7启动修复MBR(Win7+Linux删除Linux后进入grub rescue的情况)

    事因:我的笔记本原先同时安装了Win7+Linux,昨天发现硬盘实在不够用(才60G,虽然还有个500G的移动硬盘),就想把里面的Ubuntu格了.都是用虚拟机做测试的多.后来就格了Ubuntu所在的 ...

  9. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

随机推荐

  1. Node.js 介绍及安装

    Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...

  2. 单元测试工具NUnit的使用

      使用 NUnit 工具来进行单元测试 首先在要创建一个单元测试的项目,通常在原有的解决方案中添加新项目, 在弹出的项目类型中选择单元测试,项目的命名一般情况下与解决方案的名称相同后加UnitTes ...

  3. (10/24) 图片跳坑大战--处理html中的图片

    补充,在前面的服务启动执行命令中,我们在package.json中的配置信息为: "scripts": { "server": "webpack-de ...

  4. 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C

    ▶ 本章介绍了多设备胸膛下的 CUDA 编程,以及一些特殊存储类型对计算速度的影响 ● 显存和零拷贝内存的拷贝与计算对比 #include <stdio.h> #include " ...

  5. restfull 风格 参考 https://blog.csdn.net/jaryle/article/details/52141097

    https://www.cnblogs.com/xiaoxian1369/p/4332390.html :

  6. HTML5 移动端 自定义点击事件

    /* 封装的TAP事件 */ (function () { /** * IOS 和 PC 端 只需要创建一次就能一直使用 * Android 手机 每次使用的时候都需要从新创建 */ function ...

  7. 使用JSP页面生成PDF报表

    转自:http://developer.51cto.com/art/200907/134261.htm 1.iText简介 iText是一个开放源码的Java类库,可以用来方便地生成PDF文件.大家通 ...

  8. cb6xe7代码提示风格变化

  9. 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输

    进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...

  10. jsfl 改变舞台宽高

    fl.getDocumentDOM().height= 680; fl.getDocumentDOM().width= 550;