Rescue(BFS时间最短 另开数组或优先队列)
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.)
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
题目意思:Angel被抓住了,关在监狱里面,他的朋友想要去营救他,他的朋友可以上下左右移动一步,每移动一步耗时一个时间单位,监狱里还有警卫x,杀死一个警卫也要耗时一个时间单位,监狱里面还有不能走的围墙#,求他的朋友找到Angel至少需要多少时间。
解题思路:我们知道BFS适合用来求最短路,求出来的解是步数最少的解,但是这道题步数最少的解并不是最优解,我们会发现在BFS扩展节点时,单纯遇到守卫就直接对步数加一是不影响从r到a的距离的,想到这里,单纯的BFS好像就不行了,这里就需要对BFS进行一些处理,这里我们再定义一个数组mintime,mintime[i][j]代表Angel朋友走到(i,j)位置所需要的最少时间,在BFS搜索过程中,从当前位置走到临近位置(x,y)时,只有当这种走法比之前走到(x,y)位置所花时间更少,才会把当前走到(x,y)位置所代表表示的状态入队列,否则是不会入队列的。在本代码中并没有使用标明各个位置是否访问过的状态数组vis,也没有在BFS过程中将访问过的相邻位置设置为不可再访问,但BFS也不会无限搜索下去,因为从某个位置出发判断是否需要将它的相邻位置(x,y)入队列时,条件是这种走法比之前走到(x,y)位置所花时间更少;如果所花时间更少,则(x,y)位置会重复入队,但不会无穷下去,因为到达(x,y)位置的最少时间肯定是有下界的。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
struct point
{
int x;
int y;
int step;
int time;
};
queue<point>q;
char maps[][];
int mintime[][];///走到每个位置所需的最少时间
int dix[]= {,-,,};
int diy[]= {,,,-};
int ax,ay;///Angel位置
int judge(int x,int y)///判断能否移动到相邻的位置
{
if(x>=&&x<n&&y>=&&y<m&&maps[x][y]!='#')///排除边界和墙
{
return ;
}
return ;
}
int BFS(point s)
{
int i,a,b;
q.push(s);
point hd;
while(!q.empty())
{
hd=q.front();
q.pop();
for(i=; i<; i++)
{
a=hd.x+dix[i];
b=hd.y+diy[i];
if(judge(a,b))
{
point t;///向第i方向后走一步的位置
t.x=a;
t.y=b;
t.step=hd.step+;
t.time=hd.time+;
if(maps[a][b]=='x')
{
t.time++;///杀死守卫多花一个时间单位
}
if(t.time<mintime[a][b])
{
mintime[a][b]=t.time;
q.push(t);
}
}
}
}
return mintime[ax][ay];
}
int main()
{
int i,j,mint;
int sx,sy;
point start;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(maps,,sizeof(maps));
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
scanf("%c",&maps[i][j]);
mintime[i][j]=INF;
if(maps[i][j]=='a')
{
ax=i;
ay=j;
}
if(maps[i][j]=='r')
{
sx=i;
sy=j;
}
}
getchar();
}
start.x=sx;
start.y=sy;
start.step=;
start.time=;
mintime[sx][sy]=;
mint=BFS(start);
if(mint<INF)
{
printf("%d\n",mint);
}
else
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
return ;
}
这道题我还看到网上大多数的做法是使用优先队列+BFS,这里我也给出使用优先队列做法的代码与解释
使用优先队列的话,我们需要这样的一个结构体
struct point
{
int x;
int y;
int time;
bool friend operator<(point a,point b)
{
return a.time>b.time;///耗时小的优先
}
};
用time来记录移动和打败守卫的耗时,在优先队列中走到每一个位置状态中耗时少的优先入队,这样就改变了BFS原先的搜索方式了,我们知道,BFS之所以称之为广度优先搜索是因为其搜索方式是如同水波的涟漪一样,从搜索点向四周扩散,可现在的搜索方式是先向四周中耗时最少的点扩散,哪怕到了新的邻点,由于优先队列的机制,只要到新的邻点的时间比到之前邻点少,也会先去搜索新的邻点的邻点,即新的邻点的邻点在队列中的位置在之前的邻点的前面。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
struct point
{
int x;
int y;
int time;
bool friend operator<(point a,point b)
{
return a.time>b.time;///耗时小的优先
}
};
char maps[][];
int vis[][];///标明访问状态的数组
int dix[]= {,-,,};
int diy[]= {,,,-};
int ax,ay;///Angel位置
int judge(int x,int y)///判断能否移动到相邻的位置
{
if(x>=&&x<n&&y>=&&y<m&&maps[x][y]!='#'&&vis[x][y]==)///排除边界,墙和访问状态
{
return ;
}
return ;
}
int BFS(point s)
{
int i,a,b;
priority_queue<point>q;
while(!q.empty())
{
q.pop();
}///清空队列
vis[s.x][s.y]=;
q.push(s);
point hd;
while(!q.empty())
{
hd=q.top();
q.pop();
if(hd.x==ax&&hd.y==ay)
{
return hd.time;
}
for(i=; i<; i++)
{
a=hd.x+dix[i];
b=hd.y+diy[i];
if(judge(a,b))
{
point t;///向第i方向后走一步的位置
t.x=a;
t.y=b;
t.time=hd.time+;
if(maps[a][b]=='x')
{
t.time++;///杀死守卫多花一个时间单位
}
q.push(t);
vis[a][b]=;
}
}
}
return ;
}
int main()
{
int i,j,mint;
int sx,sy;
point start;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
memset(maps,,sizeof(maps));
memset(vis,,sizeof(vis));
for(i=; i<n; i++)
{
for(j=; j<m; j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='a')
{
ax=i;
ay=j;
}
if(maps[i][j]=='r')
{
sx=i;
sy=j;
}
}
getchar();
}
start.x=sx;
start.y=sy;
start.time=;
mint=BFS(start);
if(mint==)
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
else
{
printf("%d\n",mint);
}
}
return ;
}
因为看到数据量并不是很大,这道题我之前也试图使用DFS穷举加上一些剪枝来解决,可是时间超限,不过这次对DFS和BFS也有了一些新的理解了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m;
char maps[][];
int vis[][];
int dix[]={,-,,};
int diy[]={,,,-};
int ans;///记录最小耗时
int flag;///记录是否能够找到天使
void my_dfs(int x,int y,int time)///time记录所用的时间
{
int i,a,b;
if(maps[x][y]=='#'||vis[x][y]==)
{
return ;
}
if(time>=ans)///此处为一次剪枝
{
return ;
}
if(maps[x][y]=='r')
{
flag=;
if(time<ans)///找到最短的通路
{
ans=time;
}
return ;
}
if(maps[x][y]=='x')
{
time++;
}
vis[x][y]=;
for(i=;i<;i++)
{
a=x+dix[i];
b=y+diy[i];
my_dfs(a,b,time+);
}
vis[x][y]=;///消去标记
}
int main()
{
int i,j,x,y;
while(scanf("%d%d%",&n,&m)!=EOF)
{
getchar();
memset(maps,'#',sizeof(maps));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%c",&maps[i][j]);
if(maps[i][j]=='a')
{
x=i;
y=j;
}
}
getchar();
}
flag=;
ans=*;///迷宫规模
my_dfs(x,y,);
if(flag)
{
printf("%d\n",ans);
}
else
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
return ;
}
Rescue(BFS时间最短 另开数组或优先队列)的更多相关文章
- 解决N个人过桥时间最短问题(Java版本)
[问题描述] n个人要晚上过桥,在任何时候最多两个人一组过桥,每组要有一只手电筒.在这n个人中只有一个手电筒能用,因此要安排以某种往返的方式来返还手电筒,使更多的人可以过桥. 注意:每个人的过桥速 ...
- poj1649 Rescue(BFS+优先队列)
Rescue Time Limit: 2 Seconds Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...
- HDU1242 Rescue(BFS+优先队列)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- hdu1242 Rescue(BFS +优先队列 or BFS )
http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意: Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...
- 关于bfs时间轴
对于bfs,由于是通过不断将平行位置的元素加入到队列进行的,所以它在一定情况下淡化了与队列外部的 "时间" 联系观念,通过一个数组记录内部的 "时间" 这 ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- 线性时间O(n)内求数组中第k大小的数
--本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...
- hdu1372 BFS求最短路径长度
C - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bi ...
随机推荐
- Wireshark抓取Mqtt报文
安装版本较高的Wireshark,我的版本是2.4.6,然后在编辑--> 首选项--> 协议中找到MQTT,然后将端口改为你MQTT服务器的端口,然后就可以在抓包中抓到MQTT了
- 关于document.write()加载JS等静态资源 和 异步async加载JS
现流行浏览器对于静态资源的预加载 传统的浏览器,对于静态资源加载,会阻塞 HTML 解析器的线程进行,无论内联还是外链. 例如: <script src="test1.js" ...
- php_Trait
* Trait Answer* Trait : 关键字 Trait* 使用 use* 我的理解 是为了解决php不能多继承的一个处理方式* 在使用的时候 可以让两个不相关的类 产生联系* Trait ...
- spark sql cache时发现的空字符串问题
博客园首发,转帖请注明地址:https://www.cnblogs.com/tzxxh/p/10267202.html 图一 图1未做cache,直接过滤expression列的 null 和空字符串 ...
- django_ORM学生管理系统
一.新建django项目准备工作 CMD新建项目命令:django-admin startproject [项目名称] pycharm的project目录里新建app命令:python manage. ...
- English_phonetic symbol
Introduction 本人学习了奶爸课程---45天的搞定发音课,结合自己的英语水平,为自己撰写的一个系统的英语发音课,不只是音标,还有音标辨析.连读.音调等. 重点:英语发音时一个持续一生的东西 ...
- python--复习之路的目录
想要看时点链接看看,常来复习,温故而知新,可以为师矣. 1:基本类型 python--基本类型之字符串 python--基本类型之列表 python--基本类型之元组 python--基本类型之集合 ...
- Apache Tomcat 8.5 安全配置与高并发优化
通常我们在生产环境中,Tomcat的默认配置显然不能满足我们的产品需求,所以很多时候都需要对Tomcat的配置进行调优,以下综合我自己的经验来配置 Tomcat 安全与优化情况,如果你有更好的方案,请 ...
- 【BZOJ3991】寻宝游戏(动态规划)
[BZOJ3991]寻宝游戏(动态规划) 题面 BZOJ 题解 很明显,从任意一个有宝藏的点开始,每次走到相邻的\(dfs\)的节点就行了. 证明? 类似把一棵树上的关键点全部标记出来 显然是要走一个 ...
- 北京Uber优步司机奖励政策(2月17日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...