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 ...
随机推荐
- SpringBoot入门篇--读取资源文件配置
在项目的开发中,我们知道的是SpringBoot框架大大减少了我们的配置文件,但是还是留下了一个application.properties文件让我们可以进行一些配置.当然这些配置必然是包括服务器的配 ...
- uva-193-图染色-枚举
题意:n个节点,可用描成黑色或者白色,黑节点和黑节点不能相连,问最多描出多少黑节点 #include <iostream> #include <stdio.h> #includ ...
- jar包双击执行引用外部包问题
大家都知道一个java应用项目可以打包成一个jar,当然你必须指定一个拥有main函数的main class作为你这个jar包的程序入口. 具体的方法是修改jar包内目录META-INF下的MANIF ...
- sql server 定期自动清理日志
https://blog.csdn.net/dqs78833488/article/details/51372491
- angular ui.router 路由传参数
angular已经用了一段时间了,最近在做路由,做一下笔记. 路由跳转的时候进行穿参 ui.router方式 <a ui-sref="edit({id:5})"> 编辑 ...
- jsp访问java变量
jsp页面中javascript访问 java的变量 <%= JAVA变量名%> jsp中嵌入java代码<%java代码%> --变量<% String JAVASOu ...
- xe7 c++builder 日期时间头文件函数大全 date
c++builde r时间日期函数大全,在头文件System.DateUtils.hpp,不过没有IncMonth,因为这个函数定义在System.SysUtils.hpp里头了,唉 date,dat ...
- SQL 2012 分页取数据
,), data int ) select * from t1 row rows only create clustered index t1c on t1(id) declare @i int ) ...
- Spring boot&Mybatis 启动报错 Failed to auto-configure a DataSource
*************************** APPLICATION FAILED TO START *************************** Description: Fai ...
- dir 使用,统计文件数量
dir /b /a-d | find /v /c "$$$$" >1.log--※ 来源:·水木社区 newsmth.net·[FROM: 125.46.17.*] 今天去水 ...