我是靠谱客的博主 老实柠檬,最近开发中收集的这篇文章主要介绍HDOJ-1242(Rescue)(bfs+优先队列),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

HDOJ-1242(Rescue)

Problem Description
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
 
注:这题的关键是在杀警卫,并占据警卫所在的格子时,需要花费时间值为:2。由于每走一步都要花费时间值:1,所以第一感觉是用bfs()+优先队列。

My  solution:

/*2016.3.3*/

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
char map[210][210];
int mark[210][210],n,m,d[4]={0,1,-1,0},bb[4]={1,0,0,-1},ans;
struct node
{
	int x,y,step;
	friend bool operator < (node a,node b)
	{
		return a.step>b.step;//从小到大排序 
	}
}a,b;
int check(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m||mark[x][y]==1||map[x][y]=='#')
	return 0;
	return 1;
}
void  bfs(int x,int y)
{
	int i,j,k;
	priority_queue<node>q;
	memset(mark,0,sizeof(mark));
	a.x=x;
	a.y=y;
	a.step=0;
	mark[x][y]=1;//标记起点 
	q.push(a);
	while(!q.empty())
	{
		b=q.top();
		q.pop();
		if(map[b.x][b.y]=='a')
		{
			ans=b.step;
			return ;
		}
		for(i=0;i<4;i++)
		{
			a.x=b.x+bb[i];
			a.y=b.y+d[i];
			if(check(a.x,a.y))
			{
				if(map[a.x][a.y]=='x')//遇到警卫加2 
				a.step=b.step+2;
				else
				a.step=b.step+1;
				mark[a.x][a.y]=1;//容易忘记标记 
				q.push(a);
			}
		}
	}
}
int main()
{
	int i,j,k,ex,ey,sx,sy,t;
	
	while(scanf("%d%d",&n,&m)==2)
	{
		ans=0;
		for(i=0;i<n;i++)
		{
			getchar();
			for(j=0;j<m;j++)
			{
				scanf("%c",&map[i][j]);
				if(map[i][j]=='r')
				{
				   sx=i,sy=j;
				}	
			}
		 } 
		 bfs(sx,sy);
		 if(ans)
		 printf("%dn",ans);
		 else
		 printf("Poor ANGEL has to stay in the prison all his life.n");	 
	}
	return 0;
}



最后

以上就是老实柠檬为你收集整理的HDOJ-1242(Rescue)(bfs+优先队列)的全部内容,希望文章能够帮你解决HDOJ-1242(Rescue)(bfs+优先队列)所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部