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. 《opencv学习》 之 几何变换

    图像平移: 1.不改变图像大小 2.改变图像大小 编程按照目标图像的角度去编写 不改变大小的平移 1 void imageTranslation1(Mat& src, Mat& dst ...

  2. oracle库和表空间

    完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...

  3. CUDA C Programming Guide 在线教程学习笔记 Part 11

    ▶ 数学函数 ● 舍入函数,考虑被舍入参数有双精度浮点和单精度浮点,舍入方式有区别,舍入结果有整形.长整形和长长整形,所以共有以下舍入函数. // math_functions.h extern __ ...

  4. WINdows常用监控相关

    参考网址: http://www.jb51.net/article/49986.htm 一.图新Shell下: 1.    最直观的:(在运行里面输入CMD,以下命令都是在CMD下输入的:) 输入 s ...

  5. JAVA Spring JavaBean 属性值的注入方式( 属性注入, 特殊字符注入 <![CDATA[ 带有特殊字符的值 ]]> , 构造器注入 )

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 5 python 内置类

    1.实例属性和类属性 给实例绑定属性的方法是通过实例变量,或者通过self变量: class Chinese: def __init__(self,name,sex,age): self.name = ...

  7. centos7 防火墙配置

    firewall-cmd --zone=public --add-port=80/tcp --permanentfirewall-cmd --zone=public --add-port=8080/t ...

  8. JSTL标签库学习记录2-fmt

    fmt的标签为辅助性功能标签 设置编码 <fmt:requestEncoding value=""> 国际化相关 <fmt:setLocale value=&qu ...

  9. win64+anaconda+xgboost(转)

    Windows下安装python版的XGBoost(Anaconda)          XGBoost是近年来很受追捧的机器学习算法,由华盛顿大学的陈天奇提出,在国内外的很多大赛中取得很不错的名次, ...

  10. oracle系统视图字段说明

    oracle系统表v$session.v$sql表的列字段说明‍ 在本视图中,每一个连接到数据库实例中的 session都拥有一条记录.包括用户 session及后台进程如 DBWR, LGWR, a ...