#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
struct cell {
int r,c;
};
queue<cell> bee;
queue<cell> bear;
bool wall[1010][1010];
int disBear[1010][1010];
int disBee[1010][1010];
cell home;
cell start;
int di[4][2]={{0,-1},{0,1},{-1,0},{1,0}};
int n,s;
bool check(int x) {
memset(disBear,63,sizeof(disBear));
disBear[start.r][start.c]=x*s;
if (disBee[start.r][start.c]<=x) return false;
bear.push(start);
int nowr,nowc,nextr,nextc;
while (!bear.empty()) {
nowr=bear.front().r;
nowc=bear.front().c;
bear.pop();
for (int i=0;i<4;i++) {
nextr=nowr+di[i][0];
nextc=nowc+di[i][1];
if (nextc<0||nextc>=n||nextr<0||nextr>=n) continue;
if (wall[nextr][nextc]) continue;
if (disBear[nextr][nextc]<=disBear[nowr][nowc]+1) continue;
if (disBee[nextr][nextc]<=(disBear[nowr][nowc]+1)/s) continue;
disBear[nextr][nextc]=disBear[nowr][nowc]+1;
bear.push(cell{nextr,nextc});
}
}
return disBear[home.r][home.c]!=0x3f3f3f3f;
}
int main () {
scanf("%d%d",&n,&s);
char x;
memset(disBee,63,sizeof(disBee));
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
scanf(" %c",&x);
if (x=='T') wall[i][j]=true;
else if (x=='M'){
start=cell{i,j};
}
else if (x=='D') home=cell{i,j};
else if (x=='H') {
bee.push(cell{i,j});
disBee[i][j]=0;
}
}
}
int nowr,nowc;
int nextr,nextc;
while (!bee.empty()) {
nowr=bee.front().r;
nowc=bee.front().c;
bee.pop();
for (int i=0;i<4;i++) {
nextr=nowr+di[i][0];
nextc=nowc+di[i][1];
if (nextc<0||nextc>=n||nextr<0||nextr>=n) continue;
if (wall[nextr][nextc]) continue;
if (nextr==home.r&&nextc==home.c) continue;
if (disBee[nextr][nextc]<=disBee[nowr][nowc]+1) continue;
disBee[nextr][nextc]=disBee[nowr][nowc]+1;
bee.push(cell{nextr,nextc});
}
}
int lo=0;
int hi=n*n;
int mid;
while (lo<hi) {
mid=(lo+hi+1)>>1;
if (check(mid)) {
lo=mid;
} else {
hi=mid-1;
}
}
if (check(lo)) printf("%d\n",lo);
else printf("-1\n");
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:51:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&s);
~~~~~^~~~~~~~~~~~~~
mecho.cpp:58:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c",&x);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8312 KB |
Output is correct |
2 |
Correct |
12 ms |
8312 KB |
Output is correct |
3 |
Correct |
9 ms |
8368 KB |
Output is correct |
4 |
Correct |
9 ms |
8444 KB |
Output is correct |
5 |
Correct |
11 ms |
8520 KB |
Output is correct |
6 |
Correct |
12 ms |
8552 KB |
Output is correct |
7 |
Correct |
200 ms |
9368 KB |
Output is correct |
8 |
Correct |
10 ms |
9368 KB |
Output is correct |
9 |
Correct |
11 ms |
9368 KB |
Output is correct |
10 |
Correct |
10 ms |
9368 KB |
Output is correct |
11 |
Correct |
11 ms |
9368 KB |
Output is correct |
12 |
Correct |
13 ms |
9368 KB |
Output is correct |
13 |
Correct |
14 ms |
9368 KB |
Output is correct |
14 |
Correct |
15 ms |
9368 KB |
Output is correct |
15 |
Correct |
10 ms |
9368 KB |
Output is correct |
16 |
Correct |
13 ms |
9368 KB |
Output is correct |
17 |
Correct |
10 ms |
9368 KB |
Output is correct |
18 |
Correct |
10 ms |
9368 KB |
Output is correct |
19 |
Correct |
9 ms |
9368 KB |
Output is correct |
20 |
Correct |
10 ms |
9368 KB |
Output is correct |
21 |
Correct |
10 ms |
9368 KB |
Output is correct |
22 |
Correct |
13 ms |
9368 KB |
Output is correct |
23 |
Correct |
12 ms |
9368 KB |
Output is correct |
24 |
Correct |
11 ms |
9368 KB |
Output is correct |
25 |
Correct |
15 ms |
9368 KB |
Output is correct |
26 |
Correct |
13 ms |
9368 KB |
Output is correct |
27 |
Correct |
11 ms |
9368 KB |
Output is correct |
28 |
Correct |
14 ms |
9368 KB |
Output is correct |
29 |
Correct |
13 ms |
9368 KB |
Output is correct |
30 |
Correct |
12 ms |
9368 KB |
Output is correct |
31 |
Correct |
14 ms |
9368 KB |
Output is correct |
32 |
Correct |
13 ms |
9368 KB |
Output is correct |
33 |
Correct |
24 ms |
9368 KB |
Output is correct |
34 |
Correct |
22 ms |
9368 KB |
Output is correct |
35 |
Correct |
84 ms |
9368 KB |
Output is correct |
36 |
Correct |
25 ms |
9368 KB |
Output is correct |
37 |
Correct |
38 ms |
9368 KB |
Output is correct |
38 |
Correct |
85 ms |
9368 KB |
Output is correct |
39 |
Correct |
31 ms |
9368 KB |
Output is correct |
40 |
Correct |
50 ms |
9368 KB |
Output is correct |
41 |
Correct |
96 ms |
9368 KB |
Output is correct |
42 |
Correct |
34 ms |
9368 KB |
Output is correct |
43 |
Correct |
43 ms |
9368 KB |
Output is correct |
44 |
Correct |
130 ms |
9368 KB |
Output is correct |
45 |
Correct |
58 ms |
9368 KB |
Output is correct |
46 |
Correct |
40 ms |
9368 KB |
Output is correct |
47 |
Correct |
114 ms |
9368 KB |
Output is correct |
48 |
Correct |
41 ms |
9368 KB |
Output is correct |
49 |
Correct |
67 ms |
9368 KB |
Output is correct |
50 |
Correct |
150 ms |
9368 KB |
Output is correct |
51 |
Correct |
58 ms |
9368 KB |
Output is correct |
52 |
Correct |
68 ms |
9368 KB |
Output is correct |
53 |
Correct |
225 ms |
9368 KB |
Output is correct |
54 |
Correct |
53 ms |
9368 KB |
Output is correct |
55 |
Correct |
58 ms |
9368 KB |
Output is correct |
56 |
Correct |
223 ms |
9368 KB |
Output is correct |
57 |
Correct |
63 ms |
9368 KB |
Output is correct |
58 |
Correct |
86 ms |
9368 KB |
Output is correct |
59 |
Correct |
231 ms |
9368 KB |
Output is correct |
60 |
Correct |
62 ms |
9368 KB |
Output is correct |
61 |
Correct |
76 ms |
9520 KB |
Output is correct |
62 |
Correct |
329 ms |
9520 KB |
Output is correct |
63 |
Correct |
197 ms |
9520 KB |
Output is correct |
64 |
Correct |
327 ms |
9520 KB |
Output is correct |
65 |
Correct |
300 ms |
9520 KB |
Output is correct |
66 |
Correct |
248 ms |
9520 KB |
Output is correct |
67 |
Correct |
228 ms |
9520 KB |
Output is correct |
68 |
Correct |
119 ms |
9520 KB |
Output is correct |
69 |
Correct |
109 ms |
9520 KB |
Output is correct |
70 |
Correct |
163 ms |
9520 KB |
Output is correct |
71 |
Correct |
122 ms |
9520 KB |
Output is correct |
72 |
Correct |
93 ms |
9520 KB |
Output is correct |
73 |
Correct |
135 ms |
9740 KB |
Output is correct |
74 |
Correct |
203 ms |
9740 KB |
Output is correct |
75 |
Correct |
222 ms |
9740 KB |
Output is correct |
76 |
Correct |
191 ms |
9740 KB |
Output is correct |
77 |
Correct |
234 ms |
9760 KB |
Output is correct |
78 |
Correct |
190 ms |
9760 KB |
Output is correct |
79 |
Correct |
117 ms |
9760 KB |
Output is correct |
80 |
Correct |
136 ms |
9760 KB |
Output is correct |
81 |
Correct |
166 ms |
9760 KB |
Output is correct |
82 |
Correct |
162 ms |
9760 KB |
Output is correct |
83 |
Correct |
179 ms |
9760 KB |
Output is correct |
84 |
Correct |
173 ms |
9760 KB |
Output is correct |
85 |
Correct |
194 ms |
9760 KB |
Output is correct |
86 |
Correct |
190 ms |
9760 KB |
Output is correct |
87 |
Correct |
227 ms |
9760 KB |
Output is correct |
88 |
Correct |
195 ms |
9760 KB |
Output is correct |
89 |
Correct |
254 ms |
9760 KB |
Output is correct |
90 |
Correct |
217 ms |
9760 KB |
Output is correct |
91 |
Correct |
227 ms |
9760 KB |
Output is correct |
92 |
Correct |
203 ms |
9760 KB |
Output is correct |