Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 27277 Accepted Submission(s): 9668
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.)
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.
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
9
10
11
127 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
Sample Output
复制代码
1
2
3
4
513
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
思路:
经典DFS迷宫问题,可以参考之前的
解救小哈这道题,只不过这里遇到守卫的时候+2就好了,并且初始位置和重点位置一开始都要看作是.路。这里需要注意的是要剪枝。剪枝的意思就是对于一些不可能的情况就不继续走了,相当于从根芽就把这条路扼杀了,实质就是对不需要继续走下去的特殊情况的提前跳出。因为如果不剪枝,200*200的地图会超时,可以以天使为起点进行dfs,记录到达map[x][y]的最小值、到达每个a的最小值。
代码:
复制代码
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m,targetx,targety; int inf=999999; int a[201][201],book[201][201]; int minL[201][201]; //用于修剪,记录到达x,y的最小值,如果再次到达大于最小值,则说明没有必要走下去了 void dfs(int x,int y,int step) { int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}}; int dx,dy,k; if(x==targetx&&y==targety) { if(inf>step) inf=step; else return ; } //cout<<x<<" "<<y<<endl; for(int k=0;k<4;k++) { dx=x+dir[k][0]; dy=y+dir[k][1]; if(dx<1||dx>n||dy<1||dy>m)continue; if((a[dx][dy]==0||a[dx][dy]==2)&&book[dx][dy]==0) { // 剪枝 if(step+1>minL[dx][dy])continue; else minL[dx][dy] = step+1; //更新最小值 // book[dx][dy]=1; if(a[dx][dy]==2) dfs(dx,dy,step+2); else dfs(dx,dy,step+1); book[dx][dy]=0; } } return; } int main() { freopen("in.txt","r",stdin); int startx,starty; while(cin>>n>>m) { inf=999999; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char hhh; cin>>hhh; minL[i][j] = 1000000; book[i][j]=0; if(hhh=='#')a[i][j]=1; if(hhh=='.')a[i][j]=0; if(hhh=='a') { targetx=i; targety=j; a[i][j]=0; } if(hhh=='r') { startx=i; starty=j; a[i][j]=0; } if(hhh=='x') a[i][j]=2; } book[startx][starty]=1; dfs(startx,starty,0); if(inf==999999)printf("Poor ANGEL has to stay in the prison all his life.n"); else printf("%dn",inf); } return 0; }
最后
以上就是舒适宝贝最近收集整理的关于HDOJ 1242 Rescue(DFS+剪枝)Rescue的全部内容,更多相关HDOJ内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复