我是靠谱客的博主 敏感夕阳,这篇文章主要介绍HDU 1242 Rescue dfsRescue ,现在分享给大家,希望可以做个参考。

Rescue

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

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

复制代码
1
2
3
4
5
6
7
8
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........

Sample Output

复制代码
1
13

Author

CHEN, Xue

Source

ZOJ Monthly, October 2003

ACcode:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <map> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define maxn 202 #define inf 0x3f3f using namespace std; char mapp[maxn][maxn]; bool vis[maxn][maxn]; int n,m,sx,sy,time=inf; void dfs(int x,int y,int t){ if(x<=0||x>n||y<=0||y>m)return; if(t>=time)return; if(mapp[x][y]=='#')return; if(vis[x][y])return; if(mapp[x][y]=='r'){ time=time<t?time:t; return ; } if(mapp[x][y]=='x') t++; vis[x][y]=1; dfs(x+1,y,t+1);dfs(x-1,y,t+1); dfs(x,y+1,t+1);dfs(x,y-1,t+1); vis[x][y]=0; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ getchar(); for(int i=1;i<=n;++i){ for(int j=1;j<=m;++j){ mapp[i][j]=getchar(); if(mapp[i][j]=='a'){ sx=i; sy=j; } } getchar(); } memset(vis,0,sizeof(vis)); time=inf; dfs(sx,sy,0); if(time==inf) printf("Poor ANGEL has to stay in the prison all his life.n" ); else printf("%dn",time); } return 0; }


最后

以上就是敏感夕阳最近收集整理的关于HDU 1242 Rescue dfsRescue 的全部内容,更多相关HDU内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部