您的位置:首页 > 产品设计 > UI/UE

HDU Rescue

2016-04-08 18:31 330 查看

Rescue
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

13


Author

CHEN, Xue

Source

#include<cstdio>

using namespace std;

#include<cstring>

#define M 205

char map[M][M];

int visit[M][M];

int sx,sy,d;

int m,n,minl;

void DFS(int x,int y,int l){

if(!(x>=0&&x<m&&y>=0&&y<n)) //限制在图内。

return ;

if(l>=minl) return; //减支。

if(map[x][y]=='#'||visit[x][y]==1) //不能通过。

return ;

if(map[x][y]=='x')

l++; //遇守卫长度加一。

if(map[x][y]=='r'){

if(l<minl) minl=l;

return ;

}

visit[x][y]=1; //当前层标记。

DFS(x,y+1,l+1);

DFS(x,y-1,l+1);

DFS(x-1,y,l+1);

DFS(x+1,y,l+1);

visit[x][y]=0; //对初始位置无用。对>=2位置广搜做准备。

}

int main(){

int i,j;

while(scanf("%d%d ",&m,&n)!=EOF){ //注意接收回车符。用一个空格即可。

memset(visit,0,sizeof(visit)); //初始化。

for(i=0;i<m;i++){

for(j=0;j<n;j++){

map[i][j]=getchar();

if(map[i][j]=='a'){

sx=i;

sy=j;

}

}

getchar(); //吸收回车符。或用字符串接收可不吸收。

}

minl=M; //最小值初始化。赋M。以寻找最小值。

DFS(sx,sy,0);//从0开始。l 即总长度。

if(minl!=M) printf("%d\n",minl);

else printf("Poor ANGEL has to stay in the prison all his life.\n" );

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: