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 ...
随机推荐
- 浅析MySQL主从复制技术(异步复制、同步复制、半同步复制)
Preface As we all know,there're three kinds of replication in MySQL nowadays.Such as,asynchr ...
- 利用ascii码生成26个英文字母
<script> let a = ""; for (var i = 65; i < 91; i++) { a += String.fromCharCode(i); ...
- animation(动画)设置
1.animation 动画 概念:当您在 @keyframes 中创建动画时,请把它捆绑到某个选择器,否则不会产生动画效果. 通过规定至少以下两项 CSS3 动画属性,即可将动画绑定到选择器: 规定 ...
- html 页面中的 base href 和 target
它只能应用于标记<head>与</head>之间 href:网页上的所有相对路径在链接时都将在前面加上基链接指向的地址. target:—设定文件显示的窗口,同a标记中的tar ...
- MapWinGIS使用
.net语言中使用MapWinGIS.ocx http://www.cnblogs.com/kekec/archive/2011/03/30/1999709.html 基于MapWinGis的开发探索 ...
- 北京Uber优步司机奖励政策(11月30日~12月4日)
用户组:人民优步(适用于12月1日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...
- CF 1042 F. Leaf Sets
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
- 关于 NPOI 导出的 Excel 出现“部分内容有问题” 的解决方法
近期发现使用 NPOI 导出的 Excel 文件,有部分用户反映在打开时报错,测试了一下,发现在低版本的 Office 中(2003版,配合2007格式兼容包)打开正常,但在高版本 Office 中, ...
- Appium_Python_API说明
Appium_Python_API 1.contexts contexts(self): Returns the contexts within the current session. 返回当前会话 ...
- Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式
Unity编辑器 - 鼠标悬停在控件上时改变鼠标样式 摘自Unity文档 EditorGUIUtility.AddCursorRect public static void AddCursorRect ...