ZOJ 1649 Rescue

本文介绍了一个名为H-Rescue的游戏问题解决方案,通过优先队列+BFS算法实现,探讨了不同在线评测系统的差异,并提供了两种不同实现方式的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

H - Rescue
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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




优先队列+BFS。在遇到X的时候步数加1 .

但这题奇怪的是。两个OJ上的判别不一样。

貌似是HDU数据太弱。ZOJ数据比较强。

先放出最终AC两个OJ的代码。

#include <cstdio>
#include <string.h>
#include <queue>
#define MAX 205
using namespace std;
typedef struct Node{
	int t;
	int x,y;
}Node;
char map[MAX][MAX];
int vis[MAX][MAX];
int n,m;
int dir[8] = {0,1,0,-1,1,0,-1,0};
priority_queue<Node> q;
bool operator<(Node a, Node b)
{
	return a.t > b.t;
}
int BFS()
{
	Node tmp;
	int i,a,b,aa,bb,t;
	while( !q.empty() )
	{
		tmp = q.top();
		q.pop();
		a = tmp.x;
		b = tmp.y;
		t = tmp.t;
		for(i=0; i<8; i+=2)
		{
			aa = a + dir[i];
			bb = b + dir[i+1];
			if( aa >= 0 && aa < n && bb >=0 && b < m && map[aa][bb] != '#' && !vis[aa][bb] )
			{
				vis[aa][bb] = 1;
				if( map[aa][bb] == 'a' )
					return t+1;

				if( map[aa][bb] == '.' )
				{
					tmp.t = t + 1;
					tmp.x = aa;
					tmp.y = bb;
					q.push(tmp);
				}
				if( map[aa][bb] == 'x'  )
				{
					tmp.t = t + 2;
					tmp.x = aa;
					tmp.y = bb;
					q.push(tmp);
				}
			}
		}
	}
	return -1;
}
int main()
{
	int i,k,ans;
	Node tmp;
	while( ~scanf("%d%d",&n,&m) )
	{
		memset(map,0,sizeof(map));
		for(i=0; i<n; i++)
			scanf("%s",map[i]);

		while( !q.empty() )
			q.pop();
		memset(vis,0,sizeof(vis));

		for(i=0; i<n; i++)
			for(k=0; k<m; k++)
				if( map[i][k] == 'r' )
				{
					map[i][k] = '.';
					tmp.x = i;
					tmp.y = k;
					tmp.t = 0;
					q.push(tmp);
					vis[i][k] = 1;
				}
		ans = BFS();
		if( ans != -1 )
			printf("%d\n",ans);
		else
			printf("Poor ANGEL has to stay in the prison all his life.\n");
	}
return 0;
}

再放出只AC了HDU,WA在ZOJ上的代码。

#include <stdio.h>
#include <string.h>
#include <queue>
#define N 205
using namespace std;
typedef struct Node
{
        int x,y,step;
        friend bool operator<(Node a, Node b)
        {
                return a.step > b.step;
        }
}Node;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int n,m,kstartx,kstarty,ans=-1;
priority_queue<Node> q;
char input[N][N];
int vis[N][N];
int bfs(int x,int y)
{
        memset(vis,0,sizeof(vis));
        while(!q.empty())
                q.pop();
        Node fir;
        fir.x=x,fir.y=y;
        fir.step=0;
        vis[x][y]=1;
        q.push(fir);
        while(!q.empty())
        {
                Node fro=q.top();
                q.pop();
                if(input[fro.x][fro.y]=='r')
                        return fro.step;
                Node nxt;
                nxt.step=fro.step+1;
                for(int k=0;k<4;k++)
                {
                        int xx=fro.x+dx[k];
                        int yy=fro.y+dy[k];
                        if(vis[xx][yy]||input[xx][yy]=='#'||xx<0||xx>=n||yy<0||yy>=m)
                                continue;
                        vis[xx][yy]=1;
                        if(input[xx][yy]=='x')
                                nxt.step++;
                        nxt.x=xx;
                        nxt.y=yy;
                        q.push(nxt);
                }
        }
        return -1;
}
int main()
{
        while(scanf("%d%d",&n,&m)>0)
        {
                for(int i=0;i<n;i++)
                        scanf("%s",input[i]);
                for(int i=0;i<n;i++)
                        for(int j=0;j<m;j++)
                                if(input[i][j]=='a')
                                {
                                        kstartx=i,kstarty=j;
                                        break;
                                }
                ans=bfs(kstartx,kstarty);
                if(ans==-1)
                        printf("Poor ANGEL has to stay in the prison all his life.\n");
                else
                        printf("%d\n",ans);
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值