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 ...
随机推荐
- C++继承细节 -2
继承与动态内存分配 //基类定义 class BaseClass { private: char *label; public: BaseClass() {} BaseClass(const char ...
- redis参数AOF参数的bug
问题:redis 不想开启AOF了.但是还老是出现BGREWRITEAOF .(本redis版本为4.0.6) 涉及持久化参数设置如下: 排查及结果: 该redis以前开启过 AOF ,后来停止AOF ...
- HTML5新标签兼容——> <!--<if lt IE 9><!endif-->
第一种方法:(使用html5shiv) <!--[if lt IE9]> <script src="http://html5shiv.googlecode.com/svn/ ...
- go加密算法:非对称加密(一)--RSA
椭圆曲线加密__http://blog.51cto.com/11821908/2057726 // MyRas.go package main import ( "crypto/rand&q ...
- 自定义注解实现(spring aop)
1.基本概念 1.1 aop 即面向切面编程,优点是耦合性低,能使业务处理和切面处理分开开发,扩展和修改方面,当引入了注解方式时,使用起来更加方便. 1.2 应用场景 打日志.分析代码执行时间.权限控 ...
- day 20 约束 异常处理 MD5
1.类的约束(重点): 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementError # 项目经理 class Base: # 对子类进行了约束. 必须重写该方法 ...
- Qt上FFTW組件的编译与安裝
Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...
- kafka初步学习
消息系统 什么是消息系统? 消息系统负责将数据从一个应用程序传输到另一个应用程序,因此应用程序可以专注于数据,但不担心如何共享它.分布式消息传递给予可靠消息队列的概念.消息在客户端应用程序和消息传递系 ...
- 令人懊恼的阉割版fabric sdk功能缺失
按理说,fabric本身都是用golang开发的,那么fabric-sdk-go作为其亲儿子,功能应该是最为完善的.然而,与我们想法相左的是,golang版本的sdk反而是最不完备的,开发进度滞后,功 ...
- HyperLedger Fabric 1.4 基础环境搭建(7)
学习了前面几章理论知识后,本章开始介绍实践操作,先介绍Fabric基础环境搭建,采用的操作系统为Centos 7 64位,依次介绍Docker安装.Docker-Compose安装.GO语言环境安装. ...