Rescue(BFS)

B - Rescue
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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


#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
	int x,y,time;
};
node now;
char map[211][211];
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
int vis[211][211];
int m,n,a,b,a1,b1;
int judge(int x,int y){
    if(x<0||x>=n||y<0||y>=m){
        return 0;
    }
    if(map[x][y]=='#'){
        return 0;
    }
    if(vis[x][y]){
        return 0;
    }
    return 1;
}
void bfs()
{
	queue <node> q;
	now.x=a1;
	now.y=b1;
	now.time=0;
	q.push(now);
	vis[a1][b1]=1;
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		int x=now.x;
		int y=now.y;
		if(x==a&&y==b)
		{
			printf("%d\n",now.time);
			return ;
		}
		for(int i=0;i<4;i++)
		{
			x=now.x+dx[i];
			y=now.y+dy[i];
			if(!judge(x,y)){
				continue;
			}
			node end;
			if(map[x][y]=='.'||map[x][y]=='a'){
				end.x=x;
				end.y=y;
				end.time=now.time+1;
				vis[x][y]=1;
				q.push(end); 
			}else if(map[x][y]=='x'){
				end.x=x;
				end.y=y;
				end.time=now.time+2;
				vis[x][y]=1;
				q.push(end); 
			}
		}
	}
	printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
	while(~scanf("%d %d",&n,&m))
	{
		memset(vis,0,sizeof(vis));
		for(int i=0;i<n;i++)
		{
			scanf("%s",map[i]);
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='a')
				{
					a=i,b=j;
				}
				if(map[i][j]=='r')
				{
					a1=i,b1=j;
				}
			}
		}
		bfs();
	}
	return 0;
}


### 关于 PAT 甲级 1088 题目测试点 2 的解决方案 对于 PAT 甲级 1088 题目中的测试点 2,理解题目背景和具体需求至关重要。此题涉及城市间的救援行动,在给定的数据范围内寻找最优路径并计算最大救援队伍规模。 #### 数据结构的选择 为了高效处理此类图论问题,通常采用邻接表来表示城市之间的连接关系。这不仅节省空间而且便于实现广度优先搜索 (BFS) 或者深度优先搜索 (DFS),从而有效地探索所有可能的路径[^3]。 ```cpp vector<int> adj[MAX_N]; // 存储每座城市的相邻城市列表 queue<pair<int,int>> q; // BFS队列,存储当前节点及其累计人数 ``` #### 处理边界条件 考虑到可能存在多个起点或终点的情况,初始化时需特别注意这些特殊情况下的逻辑判断。当遇到无法到达目标位置的情形,则应返回特定提示信息[^4]。 ```cpp if (!found_path){ cout << "There are X accounts and no account is modified"; } ``` #### 主要算法流程 通过 BFS 来遍历整个地图,记录下每一个可以抵达的城市以及此时携带的最大救援力量数目。一旦触及目的地即刻停止搜索过程,并输出最终结果。 ```cpp while(!q.empty()){ auto [cur_city, cur_rescue_count]=q.front();q.pop(); if(cur_city==target_city){ max_rescue=max(max_rescue,cur_rescue_count); continue; } for(auto next_city : adj[cur_city]){ int new_rescue_count=min(rescue_teams[next_city],cur_rescue_count); if(visited[next_city]<new_rescue_count){ visited[next_city]=new_rescue_count; q.push({next_city,new_rescue_count}); } } } ``` 上述方法能够确保覆盖到所有潜在的有效路径,同时保持较高的时间效率,适用于本题设定内的各种输入情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值