首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【杭电oj】1242-Rescue(bfs,优先队列)

【杭电oj】1242-Rescue(bfs,优先队列)

作者头像
FishWang
发布2025-08-26 17:21:09
发布2025-08-26 17:21:09
1840
举报

Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22836 Accepted Submission(s): 8076

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

代码语言:javascript
复制
   7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........

Sample Output

代码语言:javascript
复制
   13

Author

CHEN, Xue

Source

ZOJ Monthly, October 2003

此题同 Battle City

有了前面的铺垫,这道题并没有什么难度。只有一个问题,这道题结构体变量n改成next的时候,会出现PE的现象,坑爹的改了半天。

代码如下:

代码语言:javascript
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

char map[211][211];
int vis[211][211];
int H,W;
int st_x,st_y,end_x,end_y;
int move_x[4]={0,0,1,-1};
int move_y[4]={1,-1,0,0};

struct node
{
	int x,y,stp;
	bool friend operator<(node num1,node num2)
	{
		return num1.stp>num2.stp;
	}
}a,n;

int check(int x,int y)
{
	if (x<0 || y<0 || x>=H || y>=W || vis[x][y] || map[x][y]=='#')
		return 1;
	return 0;
}

int bfs()
{
	priority_queue<node> move;
	a.x=st_x;
	a.y=st_y;
	a.stp=0;
	move.push(a);
	vis[st_x][st_y]=1;
	while (!move.empty())
	{
		a=move.top();
		move.pop();
		if (a.x==end_x && a.y==end_y)
			return a.stp;
		for (int i=0;i<4;i++)
		{
			n.x=a.x+move_x[i];
			n.y=a.y+move_y[i];
			n.stp=a.stp;
			if (check(n.x,n.y))
				continue;
			if (map[n.x][n.y]=='x')
				n.stp+=2;
			else
				n.stp++;
			vis[n.x][n.y]=1;
			move.push(n);
		}
	}
	return -1;
}

int main()
{
	while (~scanf ("%d %d",&H,&W))
	{
		for (int i=0;i<H;i++)
		{
			scanf ("%s",map[i]);
			for (int j=0;j<W;j++)
			{
				if (map[i][j]=='r')
				{
					st_x=i;
					st_y=j;
				}
				else if (map[i][j]=='a')
				{
					end_x=i;
					end_y=j;
				}
			}
		}
		memset(vis,0,sizeof(vis));
		int ans;
		ans=bfs();
		if (ans==-1)
			printf ("Poor ANGEL has to stay in the prison all his life.\n");
		else
			printf ("%d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22836 Accepted Submission(s): 8076
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档