我是靠谱客的博主 烂漫爆米花,最近开发中收集的这篇文章主要介绍ZOJ 1649 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.)

 

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

3

题意描述:r代表起始点,a代表终点.代表路,.代表空地,#代表墙,x代表卫兵。.花费一秒时间,x花费两秒时间

问从起始点r到达终点a的最少时间。

分析:BFS进行广搜,主要处理走到x为两秒时间的问题,如果遇到x,首先把它变为.也就是空地,然后加入队列,花费的时间加1,在执行后面的步骤。具体见程序实现。

代码如下:

#include<stdio.h>
#include<string.h>
int n,m,ex,ey;
char map[205][205];//地图大小
int book[205][205];//标记数组
int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//四个搜方向分别为左、下、右、上
struct node
{
int x;//横坐标
int y;//纵坐标
int f;//所用的时间
}q[43000];
int bfs(int bx,int by)
{
int tx,ty,k,tail,head;
book[bx][by]=1;//标记为1,表示走过
tail=head=0;//初始化
q[tail].x=bx;
q[tail].y=by;
q[tail].f=0;//刚开始所用时间为0
tail++;
while(head<tail)
{//队首不能超过队尾
if(q[head].x==ex&&q[head].y==ey)
return q[head].f;//找到目标a,返回所用最小时间
if(map[q[head].x][q[head].y]=='x')//如果遇到x,也就是士兵
{
map[q[head].x][q[head].y]='.';//把它变为空地
q[tail].x=q[head].x;//加入队列,因为所用时间为2秒,先处理1秒,也就是把士兵杀死
q[tail].y=q[head].y;
q[tail].f=q[head].f+1;//这时时间要加1
tail++;//队尾扩展1
head++;//队首扩展1
continue;
}
for(k=0;k<4;k++)
{//循环变量四个方向,这以后的情况也就没士兵的事了(上面已经处理过了),只有.和#了
tx=q[head].x+next[k][0];
ty=q[head].y+next[k][1];
if(tx<0||tx>=n||ty<0||ty>=m||map[tx][ty]=='#')
continue;//边界条件
if(book[tx][ty]==0)
{//如果此地还没走过 ,则加入队列
book[tx][ty]=1;
q[tail].f=q[head].f+1;
q[tail].x=tx;
q[tail].y=ty;
tail++;
}
}
head++;//此处尤为重要,没有它无法进行下面的队列扩展了
}
return -1;
}
int main()
{
int i,j,bx,by,s;
while(scanf("%d%d",&n,&m) !=EOF)
{
for(i=0;i<n;i++)
scanf("%s",map[i]);
memset(book,0,sizeof(book));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(map[i][j]=='r')
{//找到起始坐标
bx=i;
by=j;
}
if(map[i][j]=='a')
{//找到终点坐标
ex=i;
ey=j;
}
}
}
s=bfs(bx,by);
if(s==-1)
printf("Poor ANGEL has to stay in the prison all his life.n");
else
printf("%dn",s);
}
return 0;
}

 

最后

以上就是烂漫爆米花为你收集整理的ZOJ 1649 Rescue (BFS)的全部内容,希望文章能够帮你解决ZOJ 1649 Rescue (BFS)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部