Rescue
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20821 Accepted Submission(s): 7429
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
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
Author
CHEN, Xue
Source
ZOJ Monthly, October 2003
Recommend
Eddy | We have carefully selected several similar problems for you:
1240
1016
1010
1072
1253
//第一道这样的题,思路大致了解:
用最少时间找到天使 → → 天使找到最近朋友用的时间;
复制代码
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
1521 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 8 int dir[4][2]={-1, 0, 1, 0, 0, -1, 0, 1}; 9 char map[220][220]; int vis[220][220]; 10 int i, j, m, n; 11 12 struct prision 13 { 14 int x, y, time; 15 friend bool operator < (prision x, prision y) 16 { 17 return x.time > y.time; //从小 → 大; 18 } 19 } current, next; 20 21 22 int BFS(int x,int y) 23 { 24 25 26 priority_queue <prision>q; 27 prision current,next; 28 memset(vis,0,sizeof(vis)); 29 30 current.x=x; 31 current.y=y; 32 current.time=0; 33 vis[current.x][current.y]=1; 34 q.push(current); 35 36 37 while(!q.empty()) 38 { 39 40 current=q.top(); 41 q.pop(); 42 for(int i=0;i<4;i++) 43 { 44 next.x=current.x+dir[i][0]; 45 next.y=current.y+dir[i][1]; 46 47 if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!='#'&&!vis[next.x][next.y]) 48 { 49 50 if(map[next.x][next.y]=='r') 51 return current.time+1; 52 53 if(map[next.x][next.y]=='x') 54 next.time=current.time+2; 55 else 56 next.time=current.time+1; 57 vis[next.x][next.y]=1; 58 q.push(next); 59 } 60 } 61 } 62 return -1; 63 } 64 65 66 int main() 67 { 68 prision angle; 69 while(~scanf("%d %d", &n, &m)) 70 { 71 for(i=0; i<n; i++) 72 { 73 for(j=0; j<m; j++) 74 { 75 cin >> map[i][j]; 76 if(map[i][j] == 'a') 77 { 78 angle.x = i; 79 angle.y = j; 80 } 81 } 82 } 83 int time = BFS(angle.x, angle.y); 84 85 if(time == -1) 86 cout <<"Poor ANGEL has to stay in the prison all his life."<<endl; 87 else 88 cout <<time<< endl; 89 } 90 return 0; 91 }


复制代码
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
2751 #include <queue> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 int n, m; 8 //const int maxn = 220; 9 int vis[220][220]; char map[220][220]; 10 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; 11 12 struct male 13 { 14 int x, y, step; 15 friend bool operator < (male x, male y) 16 { 17 return x.step > y.step; 18 } 19 } s, t; 20 21 int Bfs(int x, int y) 22 { 23 // int nx, ny; 24 25 priority_queue <male> q; 26 memset(vis, 0, sizeof(vis)); 27 vis[x][y] = 1; 28 s.x = x; s.y = y; s.step = 0; 29 q.push(s); 30 male now; 31 while(!q.empty()) 32 { 33 t = q.top(); q.pop(); 34 for(int i=0; i<4; i++) 35 { 36 now.x = t.x + ac[i][0]; 37 now.y = t.y + ac[i][1]; 38 if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < m && !vis[now.x][now.y] && map[now.x][now.y] != '#') 39 { 40 if(map[now.x][now.y] == 'r') 41 return t.step + 1; 42 else if(map[now.x][now.y] == '.') 43 now.step = t.step + 1; 44 else 45 now.step = t.step + 2; 46 vis[now.x][now.y] = 1; 47 q.push(now); 48 } 49 } 50 } 51 return -1; 52 } 53 int main() 54 { 55 male angle; 56 int i, j, x, y; 57 while(~scanf("%d %d", &n, &m)) 58 { 59 for(int i = 0; i < n; i++) 60 { 61 for(int j = 0; j < m; j++) 62 { 63 cin >> map[i][j]; 64 if(map[i][j] == 'a') 65 { 66 angle.x = i; angle.y = j; 67 } 68 } 69 } 70 int time = Bfs(angle.x, angle.y); 71 if(time == -1) 72 printf("Poor ANGEL has to stay in the prison all his life.n"); 73 else 74 printf("%dn", time); 75 } 76 return 0; 77 } 78 79 /*#include <queue> 80 #include <cstdio> 81 #include <cstring> 82 #include <iostream> 83 using namespace std; 84 const int maxn = 220; 85 char map[maxn][maxn]; 86 int m, n, vis[maxn][maxn]; 87 int ac[4][2] = {0, 1, 0, -1, -1, 0, 1, 0}; 88 89 struct male 90 { 91 int x, y, t; 92 bool friend operator < (male x, male y) 93 { 94 return x.t > y.t; 95 } 96 }s, t, now; 97 98 int Bfs(int x,int y) 99 { 100 memset(vis, 0, sizeof(vis)); 101 priority_queue <male> q; 102 vis[x][y] = 1; 103 s.x = x; s.y = y; s.t = 0; 104 q.push(s); 105 while(!q.empty()) 106 { 107 t = q.top(); q.pop(); 108 for(int i = 0; i < 4; i++) 109 { 110 now.x = t.x + ac[i][0]; 111 now.y = t.y + ac[i][1]; 112 // printf("%d %dn", now.x, now.y); 113 if(now.x >= 0 && now.x < m && now.y >= 0 && now.y < n && !vis[now.x][now.y] && map[now.x][now.y] != '#') //容易错; 114 { 115 if(map[now.x][now.y] == 'r') 116 return t.t + 1; 117 else if(map[now.x][now.y] == '.') 118 now.t = t.t + 1; 119 else 120 now.t = t.t + 2; 121 vis[now.x][now.y] = 1; // so important ; 122 q.push(now); 123 } 124 } 125 } 126 return -1; 127 } 128 129 int main() 130 { 131 int x, y; 132 while(~scanf("%d %d", &m, &n)) 133 { 134 for(int i = 0; i < m; i++) 135 { 136 for(int j = 0; j < n; j++) 137 { 138 cin >> map[i][j]; 139 if(map[i][j] == 'a') 140 { 141 x = i; y = j; 142 } 143 } 144 } 145 //printf("%d %dn", x, y); 146 int time = Bfs(x, y); 147 if(time == -1) 148 printf("Poor ANGEL has to stay in the prison all his life.n"); 149 else 150 printf("%dn", time); 151 } 152 return 0; 153 }*/
转载于:https://www.cnblogs.com/soTired/p/4690331.html
最后
以上就是称心耳机最近收集整理的关于杭电1242--Rescue(BFS+优先队列)Rescue的全部内容,更多相关杭电1242--Rescue(BFS+优先队列)Rescue内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复