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.)

Input

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时间最短 另开数组或优先队列)的更多相关文章

  1. 解决N个人过桥时间最短问题(Java版本)

    [问题描述] n个人要晚上过桥,在任何时候最多两个人一组过桥,每组要有一只手电筒.在这n个人中只有一个手电筒能用,因此要安排以某种往返的方式来返还手电筒,使更多的人可以过桥.   注意:每个人的过桥速 ...

  2. poj1649 Rescue(BFS+优先队列)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  3. HDU1242 Rescue(BFS+优先队列)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  4. hdu1242 Rescue(BFS +优先队列 or BFS )

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 题意:     Angel被传说中神秘的邪恶的Moligpy人抓住了!他被关在一个迷宫中.迷宫的长.宽不超 ...

  5. 关于bfs时间轴

    对于bfs,由于是通过不断将平行位置的元素加入到队列进行的,所以它在一定情况下淡化了与队列外部的  "时间"   联系观念,通过一个数组记录内部的 "时间" 这 ...

  6. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  8. 线性时间O(n)内求数组中第k大小的数

    --本文为博主原创,转载请注明出处 因为最近做的WSN(wireless sensor network)实验要求用3个传感器节点接受2000个包的数据并算出一些统计量,其中就有算出中位数这么一个要求, ...

  9. hdu1372 BFS求最短路径长度

    C - 广搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

随机推荐

  1. eclipse中误删tomcat后,文件都报错,恢复server时无法选择tomcat7.0解决办法

    创建Tomcat v7.0 Server 不能进行下一步. 解决方法: 1.退出 eclipse 2.到[工程目录下]/.metadata/.plugins/org.eclipse.core.runt ...

  2. 模板——最小生成树prim算法&&向前星理解

    通过最小生成树(prim)和最短路径优化引出的向前星存图,时至今日才彻底明白了.. head[i]存储的是父节点为i引出的最后一条边的编号, next负责把head[i]也就是i作为父节点的所有边连接 ...

  3. chromium之message_pump_win之一

    写了22篇博文,终于到这里了———— MessagePumpWin!!! MessagePumpWin这个类还是挺复杂的,可以分成好几部分.接下来分块分析 从介绍看,MessagePumpWin 是M ...

  4. linux系统基础之---文件系统(基于centos7.4 1708)

  5. MySQL5.7主从同步--点位方式及GTID方式

    MySQL5.6加入了GTID的新特性,其全称是Global Transaction Identifier,可简化MySQL的主从切换以及Failover.GTID用于在binlog中唯一标识一个事务 ...

  6. Linux上搭建svn资源库

    一.安装 centos上安装 使用命令svn  --version查看是否安装过svn: 如果出现  bash: svn: command not found  则显示没有安装 可以使用 yum in ...

  7. python+selenium实现自动抢票

    使用说明 程序运行开始,需要输入出发地,目的地,出发时间,乘客信息,车次:乘客信息和车次可以输入多个 刚刚开始学习爬虫,selenium仅仅是解放了双手,运行效率不是很高: 程序运行时会打开chrom ...

  8. [转]Visual C++ 和 C++ 有什么区别?

    注:本篇内容转载与网络,方便自己学习,如有侵权请您联系我删除,谢谢. 有位同学问我“Visual C++和C++有什么区别?”,这的确是初学者会感到困惑的问题,比较常见.除此之外,还有“先学C++好, ...

  9. vue实现图片路径传送

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  10. IIC总线协议和时序

    IIC标准速率为100Kbit/s,快速模式400Kbit/s,支持多机通信,支持多主控模块,但是同一时刻只允许有一个主控.由数据线SDA和时钟SCL构成串行总线:每个电路模块都有唯一地址.I2C设备 ...