我是靠谱客的博主 老实电脑,最近开发中收集的这篇文章主要介绍ZOJ 1649: Rescue(BFS),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=649

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

题意分析:

一个人被抓进了监狱,现在他的盆友要去救他, 每次只能上下左右移动,每移动一次需要1分钟, 监狱中可能会有守卫, 杀死一个守卫需要1分钟,也就是说想到达一个有守卫的地方需要2分钟,假设能够杀死所有守,求能救人所需要的最短时间。

解题思路:

遍历所有能够走到的格子,需要注意的是每次杀死一个守卫需要的时间更长,所以不直接加入原队列之中,如果直接加入可能后面会先处理这个步数更长的,而忽略了后面加入的只加1步的。

#include <stdio.h>
#include <string.h>
#include <queue>
#define N 220
using namespace std;
int n, m;
char str[N][N];
bool book[N][N];
struct date{
	int x;
	int y;
	int temp;
};
int bfs(int x, int y)
{
	int tx, ty;
	int nex[4][2]={0,1, 0,-1, 1,0, -1,0};
	memset(book, false, sizeof(book));
	queue<date>q, p;
	q.push(date{x, y, 0});
	book[x][y]=true;
	while(!q.empty() || !p.empty())
	{
		date u, v;
		if(q.empty())
		{
			u=p.front();
			p.pop();
		}
		else if(p.empty())
		{
			u=q.front();
			q.pop();
		}
		else
		{
			u=q.front();
			v=p.front();
			if(u.temp>v.temp)
			{
				u=v;
				p.pop();
			}
			else
				q.pop();
		}	
		if(str[u.x][u.y]=='a')
			return u.temp;
			
		for(int i=0; i<4; i++)
		{
			tx=u.x+nex[i][0];
			ty=u.y+nex[i][1];
			if(tx>=n || ty>=m || tx<0 || ty<0)
				continue;
			if(str[tx][ty]=='#')
				continue;
			if(str[tx][ty]=='.' && book[tx][ty]==false)
			{
				book[tx][ty]=true;
				q.push({tx, ty, u.temp+1});	
			}
			if(str[tx][ty]=='x' && book[tx][ty]==false)
			{
				book[tx][ty]=true;
				p.push({tx, ty, u.temp+2});
			}
			if(str[tx][ty]=='a')
				return u.temp+1;
		}
	}
	return -1;
}
int main()
{
	int i, j, ans;
	while(scanf("%d%d", &n, &m)!=EOF)
	{
		ans=-1;
		for(i=0; i<n; i++)
			scanf("%s", str[i]);
		for(i=0; i<n; i++)
			for(j=0; j<m; j++)
				if(str[i][j]=='r')
				{
					ans=bfs(i, j);
					break;
				}
		if(ans==-1)
			printf("Poor ANGEL has to stay in the prison all his life.n");
		else
			printf("%dn", ans); 
		
	}
	return 0;
}

 

最后

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

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部