#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int N = 1001;
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] = inf;
if(T * S >= d[mecho.first][mecho.second]) return false;
q.push(mecho);
dist[mecho.first][mecho.second] = T * S;
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] + 1;
if(arrival >= d[tx][ty] || arrival > dist[tx][ty]) continue;
dist[tx][ty] = dist[u.first][u.second] + 1;
q.push({tx,ty});
}
}
return dist[home.first][home.second] != inf;
}
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] + S) continue;
d[tx][ty] = d[u.first][u.second] + S;
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;
} printf("%d\n", ans);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:41: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:43: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 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
292 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Runtime error |
328 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
888 KB |
Output is correct |
13 |
Runtime error |
435 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
14 |
Runtime error |
448 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
480 KB |
Output is correct |
17 |
Correct |
2 ms |
508 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
632 KB |
Output is correct |
20 |
Correct |
2 ms |
632 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
632 KB |
Output is correct |
23 |
Correct |
2 ms |
760 KB |
Output is correct |
24 |
Correct |
2 ms |
764 KB |
Output is correct |
25 |
Correct |
3 ms |
888 KB |
Output is correct |
26 |
Correct |
2 ms |
888 KB |
Output is correct |
27 |
Correct |
2 ms |
888 KB |
Output is correct |
28 |
Correct |
2 ms |
888 KB |
Output is correct |
29 |
Correct |
2 ms |
888 KB |
Output is correct |
30 |
Correct |
2 ms |
1016 KB |
Output is correct |
31 |
Correct |
2 ms |
1016 KB |
Output is correct |
32 |
Correct |
2 ms |
1016 KB |
Output is correct |
33 |
Correct |
8 ms |
3448 KB |
Output is correct |
34 |
Correct |
8 ms |
3576 KB |
Output is correct |
35 |
Runtime error |
304 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
36 |
Correct |
9 ms |
3960 KB |
Output is correct |
37 |
Correct |
9 ms |
3960 KB |
Output is correct |
38 |
Runtime error |
304 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
39 |
Correct |
10 ms |
4472 KB |
Output is correct |
40 |
Correct |
11 ms |
4476 KB |
Output is correct |
41 |
Runtime error |
289 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
42 |
Correct |
13 ms |
4988 KB |
Output is correct |
43 |
Correct |
13 ms |
4900 KB |
Output is correct |
44 |
Runtime error |
285 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
45 |
Correct |
13 ms |
5468 KB |
Output is correct |
46 |
Correct |
14 ms |
5496 KB |
Output is correct |
47 |
Runtime error |
284 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
48 |
Correct |
16 ms |
5880 KB |
Output is correct |
49 |
Correct |
17 ms |
5884 KB |
Output is correct |
50 |
Runtime error |
295 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
51 |
Correct |
19 ms |
6392 KB |
Output is correct |
52 |
Correct |
20 ms |
6392 KB |
Output is correct |
53 |
Runtime error |
307 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
54 |
Correct |
21 ms |
7032 KB |
Output is correct |
55 |
Correct |
23 ms |
7032 KB |
Output is correct |
56 |
Runtime error |
282 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
57 |
Correct |
24 ms |
7544 KB |
Output is correct |
58 |
Correct |
26 ms |
7544 KB |
Output is correct |
59 |
Runtime error |
298 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
60 |
Correct |
26 ms |
7932 KB |
Output is correct |
61 |
Correct |
28 ms |
8056 KB |
Output is correct |
62 |
Runtime error |
279 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
63 |
Correct |
119 ms |
7928 KB |
Output is correct |
64 |
Correct |
201 ms |
8056 KB |
Output is correct |
65 |
Correct |
156 ms |
8036 KB |
Output is correct |
66 |
Correct |
133 ms |
8040 KB |
Output is correct |
67 |
Correct |
126 ms |
8032 KB |
Output is correct |
68 |
Correct |
55 ms |
8064 KB |
Output is correct |
69 |
Correct |
57 ms |
7992 KB |
Output is correct |
70 |
Correct |
41 ms |
8056 KB |
Output is correct |
71 |
Correct |
45 ms |
8056 KB |
Output is correct |
72 |
Correct |
34 ms |
8056 KB |
Output is correct |
73 |
Runtime error |
495 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
74 |
Runtime error |
496 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
75 |
Runtime error |
530 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
76 |
Runtime error |
507 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
77 |
Runtime error |
503 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
78 |
Runtime error |
447 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
79 |
Runtime error |
544 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
80 |
Runtime error |
434 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
81 |
Runtime error |
438 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
82 |
Runtime error |
480 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
83 |
Runtime error |
395 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
84 |
Runtime error |
401 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
85 |
Runtime error |
416 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
86 |
Runtime error |
416 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
87 |
Runtime error |
374 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
88 |
Runtime error |
351 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
89 |
Runtime error |
337 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
90 |
Runtime error |
336 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
91 |
Runtime error |
334 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
92 |
Runtime error |
369 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |