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
//Must so
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,x) memset(a,x,sizeof(a))
#define inf 1<<29
#define NN 1000006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
/**********************************************************************
题意:
给出图,‘a’是终点,‘r’是起点,‘.’表示可以走,‘#’不能走
每走一步用1秒,遇到‘x’多花1秒
问从起点到终点最少时间
dfs深度搜索
=、=最后才发现,起点‘r’不止一个
所以干脆从终点去找最近的起点
**********************************************************************/
int tx,ty;
int row,cow;//行和列
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
char mp[202][202];
int ans;
void dfs(int x,int y,int l)//返回从该点到达终点的最短时间
{
bool fd = 0;
if (mp[x][y] == 'x')//这个地方回溯注意一下
fd = 1;
mp[x][y] = '#';
for (int i = 0;i < 4;i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if (xx >= 0&&yy >= 0&&xx < row&&yy < cow)
{
if (mp[xx][yy] == '.')
{
dfs(xx,yy,l+1);
}
if (mp[xx][yy] == 'x')
{
dfs(xx,yy,l+2);
}
if (mp[xx][yy] == 'r')
{
ans = min(l+1,ans);
}
}
}
mp[x][y] = '.';
if (fd) mp[x][y] = 'x';
}
int main()
{
while (~scanf("%d%d ",&row,&cow))
{
for (int i = 0;i < row;i++)
{
scanf("%s",mp[i]);
}
for (int i = 0;i < row;i++)
{
for (int j = 0;j < cow;j++)
{
if (mp[i][j] == 'a')
tx = i,ty = j;
}
}
ans = inf;
dfs(tx,ty,0);
if (ans == inf) puts("Poor ANGEL has to stay in the prison all his life.");
else printf("%d\n",ans);
}
return 0;
}