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. HTML5中常用的标签(及标签的属性和作用)

    1.标签:<!DOCTYPE>作用:声明是文档中的第一成分,位于<html>标签之前. 2.标签:<html>作用:此元素可告知浏览器其自身是一个HTML文档.属性 ...

  2. node.js入门基础

    内容: 1.node.js介绍 2.node.js内置常用模块 3.node.js数据交互 一.node.js介绍 (1)node.js特点 与其他语言相比,有以下优点: 对象.语法和JavaScri ...

  3. python之集合操作

    1.集合的增操作 add(item):增加至集合中 update(set):  若不存在,更新至集合中 2.集合的删操作(五种) pop():  随机删除并返回值 remove(item): 删除va ...

  4. 0_Simple__simplePitchLinearTexture

    对比设备线性二维数组和 CUDA 二维数组在纹理引用中的效率 ▶ 源代码.分别绑定相同大小的设备线性二维数组和 CUDA 二维数组为纹理引用,做简单的平移操作,重复若干次计算带宽和访问速度. #inc ...

  5. 局部敏感哈希-Locality Sensitivity Hashing

    一. 近邻搜索 从这里开始我将会对LSH进行一番长篇大论.因为这只是一篇博文,并不是论文.我觉得一篇好的博文是尽可能让人看懂,它对语言的要求并没有像论文那么严格,因此它可以有更强的表现力. 局部敏感哈 ...

  6. 很详细的curl命令使用大全

    可以看作命令行浏览器 1.开启gzip请求 curl -I http://www.sina.com.cn/ -H Accept-Encoding:gzip,defalte 2.监控网页的响应时间 cu ...

  7. leetcode404

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  8. VBA 使用QueryTables 中文乱码的处理

    一般情况: cnn = "OLEDB;Provider=IBMDA400;Data Source=TFB4001;User ID=;Password=;" Sql = " ...

  9. sendkeys

    1)为了指定单一键盘字符,必须按字符本身的键.例如,为了表示字母 A,可以用 "A" 作为 string.为了表示多个字符,就必须在字符后面直接加上另一个字符.例如,要表示 A.B ...

  10. Spring mvc RequestContextHolder分析

    转载: http://blog.csdn.net/zzy7075/article/details/53559902