这个题看上去比较简单


两个注意:1、直接时间加2不行,得标记后入队两次。

2、struct加入队列时要注意规范,我就在这里出现未知错误,规范后就行了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<set>
#include<cstdlib>
#include<cstring>

using namespace std;

#define N 222
int n,m;
char  map[N][N];
bool vist[N][N];
int ans;
int a[8][2]={{1,0},{0,1},{-1,0},{0,-1}};

struct my
{
	int x,y;
	int t;
	bool stop;
	my(int a=0,int b=0,int c=0,bool d=false)
	{
		x=a;
		y=b;
		t=c;
		stop=d;
	}

}s,d;

bool in(int x,int y)
{
	if (x>=0&&x<n&&y>=0&&y<m)
		return true;
	return false;
}

void bfs()
{
	int xx,yy;
	int i,j,k;
	queue<my> q;
	while (!q.empty())
		q.pop();

	map[s.x][s.y]='#';
	
	q.push(my(s.x,s.y,0,false));
	while (!q.empty())
	{
		my cur = q.front();
	
		q.pop();
	
		
		
		if (cur.stop)
		{
			q.push(my(cur.x,cur.y,cur.t+1,false));
			continue;
		}
		

		for (i=0;i<4;i++)
		{
			xx=cur.x+a[i][0];
			yy=cur.y+a[i][1];
			if (in(xx,yy))
			{
				if  (map[xx][yy]=='.')
				{
					map[xx][yy]='#';
		
					q.push(my(xx,yy,cur.t+1,false));
				}
				else if (map[xx][yy]=='x')
				{
					map[xx][yy]='#';
					
			
					q.push(my(xx,yy,cur.t+1,true));
				}
				else if (map[xx][yy]=='r')
				{
					ans=cur.t+1;
					map[xx][yy]='#';
					return ;
				}
			}
		}
	}
	
}

int main()
{
	freopen("in.txt","r",stdin);

	int i,j,k;

	while (scanf("%d%d",&n,&m)!=EOF)
	{
		for (i=0;i<n;i++)
			scanf("%s",map+i);

		//memset(vist,false,sizeof(vist));
	
		ans=0;
		for (i=0;i<n;i++)
		{
			for (j=0;j<m;j++)
			{
				if (map[i][j]=='a')
					s=my(i,j,0,false);
				if (map[i][j]=='r')
					d=my(i,j,0,false);
			}
		}
		ans=100000000;

		bfs();

		if (ans<10000000)
			printf("%d\n",ans);
		else
			puts("Poor ANGEL has to stay in the prison all his life.");
	}
	return 0;
}



C - Rescue


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


Submit Status


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