#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N = 802;
const int inf = 2e9;
int n, S, d[N][N], dist[N][N];
char s[N][N];
queue<ii> q;
ii mecho, home;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
bool can(int T) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
dist[i][j] = -1;
if(d[mecho.first][mecho.second] <= T) return false;
q.push(mecho);
dist[mecho.first][mecho.second] = 0;
ii u; int arrival, tx, ty;
while(!q.empty()) {
u = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
tx = u.first + dx[i], ty = u.second + dy[i];
if(min(tx,ty) < 0 || max(tx,ty) >= n) continue;
if(s[tx][ty] != 'G' && s[tx][ty] != 'D') continue;
arrival = dist[u.first][u.second] / S + 1 + T;
if(arrival >= d[tx][ty] || dist[tx][ty] != -1) continue;
dist[tx][ty] = dist[u.first][u.second] + 1;
q.push({tx,ty});
}
}
return dist[home.first][home.second] != -1;
}
int main() {
scanf("%d %d", &n, &S);
for(int i = 0; i < n; i++)
scanf(" %s", s[i]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
d[i][j] = inf;
if(s[i][j] == 'M') mecho = {i,j};
else if(s[i][j] == 'D') home = {i,j};
}
s[mecho.first][mecho.second] = 'G';
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(s[i][j] == 'H') {
q.push({i,j});
d[i][j] = 0;
}
while(!q.empty()) {
ii u = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
int tx = u.first + dx[i], ty = u.second + dy[i];
if(min(tx,ty) < 0 || max(tx,ty) >= n) continue;
if(s[tx][ty] != 'G') continue;
if(d[tx][ty] < d[u.first][u.second] + 1) continue;
d[tx][ty] = d[u.first][u.second] + 1;
q.push({tx,ty});
}
}
int lo = 0, hi = n + n, ans = -1;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
if(can(mid)) ans = mid, lo = mid+1;
else hi = mid-1;
} if(ans == -1) puts("-1");
else printf("%d\n", ans + 1);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:40: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:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %s", s[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
296 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Runtime error |
334 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
13 |
Incorrect |
8 ms |
1016 KB |
Output isn't correct |
14 |
Correct |
173 ms |
6344 KB |
Output is correct |
15 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
636 KB |
Output isn't correct |
22 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
632 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
760 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
888 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
788 KB |
Output isn't correct |
33 |
Incorrect |
13 ms |
2808 KB |
Output isn't correct |
34 |
Incorrect |
18 ms |
2808 KB |
Output isn't correct |
35 |
Runtime error |
274 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
36 |
Incorrect |
17 ms |
3192 KB |
Output isn't correct |
37 |
Incorrect |
33 ms |
3196 KB |
Output isn't correct |
38 |
Runtime error |
282 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
39 |
Incorrect |
21 ms |
3576 KB |
Output isn't correct |
40 |
Incorrect |
29 ms |
3448 KB |
Output isn't correct |
41 |
Runtime error |
279 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Incorrect |
25 ms |
3832 KB |
Output isn't correct |
43 |
Incorrect |
35 ms |
3960 KB |
Output isn't correct |
44 |
Runtime error |
290 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
45 |
Incorrect |
32 ms |
4288 KB |
Output isn't correct |
46 |
Incorrect |
45 ms |
4216 KB |
Output isn't correct |
47 |
Runtime error |
290 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
48 |
Incorrect |
42 ms |
4600 KB |
Output isn't correct |
49 |
Incorrect |
57 ms |
4600 KB |
Output isn't correct |
50 |
Runtime error |
290 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
51 |
Incorrect |
44 ms |
4860 KB |
Output isn't correct |
52 |
Incorrect |
73 ms |
5112 KB |
Output isn't correct |
53 |
Runtime error |
283 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
54 |
Incorrect |
51 ms |
5240 KB |
Output isn't correct |
55 |
Incorrect |
74 ms |
5368 KB |
Output isn't correct |
56 |
Runtime error |
304 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Incorrect |
58 ms |
5624 KB |
Output isn't correct |
58 |
Incorrect |
85 ms |
5624 KB |
Output isn't correct |
59 |
Runtime error |
283 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
60 |
Incorrect |
65 ms |
6008 KB |
Output isn't correct |
61 |
Incorrect |
98 ms |
6008 KB |
Output isn't correct |
62 |
Runtime error |
290 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
63 |
Incorrect |
108 ms |
6008 KB |
Output isn't correct |
64 |
Correct |
172 ms |
6008 KB |
Output is correct |
65 |
Correct |
191 ms |
6136 KB |
Output is correct |
66 |
Incorrect |
135 ms |
6048 KB |
Output isn't correct |
67 |
Correct |
135 ms |
6008 KB |
Output is correct |
68 |
Incorrect |
47 ms |
5984 KB |
Output isn't correct |
69 |
Correct |
42 ms |
6008 KB |
Output is correct |
70 |
Correct |
37 ms |
6008 KB |
Output is correct |
71 |
Correct |
41 ms |
6008 KB |
Output is correct |
72 |
Incorrect |
33 ms |
6008 KB |
Output isn't correct |
73 |
Runtime error |
530 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
74 |
Runtime error |
509 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
75 |
Runtime error |
532 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
76 |
Runtime error |
516 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
77 |
Runtime error |
535 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
78 |
Runtime error |
474 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
79 |
Runtime error |
454 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
80 |
Runtime error |
452 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
81 |
Runtime error |
439 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
82 |
Runtime error |
421 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
83 |
Runtime error |
359 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
84 |
Runtime error |
355 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
85 |
Runtime error |
359 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
86 |
Runtime error |
383 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
87 |
Runtime error |
363 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
88 |
Runtime error |
320 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
89 |
Runtime error |
317 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
90 |
Runtime error |
341 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
91 |
Runtime error |
323 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
92 |
Runtime error |
321 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |