HDOJ1242 Rescue(营救) 搜索
Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22286 Accepted Submission(s): 7919
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.)
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.
在BFS的搜索过程中,不能一判断出到达目标位置就退出BFS过程,否则求出来的仅仅只是r到a的最小步数。因为BFS所搜索的顶点都是按深度进行搜索的,所以BFS先搜索到的都是步数最少的,不一定是最优解,所用的时间可能更长。一定要等到链表为空,BFS搜索过程全部结束才能得出最优解或者得出无法找到目标位置的结论。
#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(营救) 搜索的更多相关文章
- HDU 1242 Rescue 营救天使
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- ZH奶酪:【数据结构与算法】搜索之BFS
1.目标 通过本文,希望可以达到以下目标,当遇到任意问题时,可以: 1.很快建立状态空间: 2.提出一个合理算法: 3.简单估计时空性能: 2.搜索分类 2.1.盲目搜索 按照预定的控制策略进行搜索, ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 搜索专题: HDU1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 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 ...
- Win7启动修复(Ubuntu删除后进入grub rescue的情况)
起因:装了win7,然后在另一个分区里装了Ubuntu.后来格掉了Ubuntu所在的分区.系统启动后出现命令窗口:grub rescue:_ 正确的解决方式: 1.光驱插入win7安装盘或者用USB启 ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- Win7启动修复MBR(Win7+Linux删除Linux后进入grub rescue的情况)
事因:我的笔记本原先同时安装了Win7+Linux,昨天发现硬盘实在不够用(才60G,虽然还有个500G的移动硬盘),就想把里面的Ubuntu格了.都是用虚拟机做测试的多.后来就格了Ubuntu所在的 ...
- ZOJ 1649:Rescue(BFS)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
随机推荐
- IDEA在编辑时提示could not autowire
IDEA在编辑时提示could not autowire 原创 2016年05月14日 10:53:38 28338 在开发中我再applicationContext-dao.xml中加入了mappe ...
- c++官方文档-class
#include <iostream> using namespace std; class Circle { double radius; public: Circle(double r ...
- centos已经安装了libestr但在安装libee时却提示未安装
在loganalyzer+rsyslog日志分析错误总结;
- 深入浅出理解依赖注入这种由外部负责其依赖需求的行为,我们可以称其为 “控制反转(IoC)”
原文地址: http://www.insp.top/learn-laravel-container ,转载务必保留来源,谢谢了! 这个组件现在可以很简单的获取到它所需要的服务,服务采用延迟加载的方式, ...
- PHP中间件--ICE
ICE(Internet Communications Engine)是Zeroc提供的一款高性能的中间件.使用ICE能使得php(或c++,java,python)与java,c++,.net,py ...
- Eclipse 中yml自动提示功能相关设置
转自:https://blog.csdn.net/lililuni/article/details/82849376
- Caused by: java.lang.IllegalStateException: Expected raw type form of org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$Match
spring 4.0.2,mybatis 3.2.6,aspectjweaver 1.8.10 使用的时候,报错: Caused by: java.lang.IllegalStateException ...
- 浅谈Matcher和pattern的使用
这两个类位于java.util.regex包下,主要用于实现正则表达式 Pattern类用于创建一个正则表达式,也可以说是创建一个匹配模式 两个静态方法创建:compile(String regex) ...
- PHP图像 因其本身有错无法显示
昨天终于将客户的一个网站迁移至虚拟主机上,满怀希望的敲入网址.唰的一声,网站很轻松的被打开了. 心里那个高兴啊~~~ 咦,怎么产品图片都没有显示出来.一块块都是空白.敲入img src对应的地址,看看 ...
- 进程队列(Queue),Pipe(管道), Manager 进行进程之间的数据传递和传输
进程Queue,实现进程传输的队列 1.Queue from multiprocessing import Process, Queue def f(q): q.put('1') q.put('2') ...