#include <stdio.h>
#define N 800
int di[] = { -1, 1, 0, 0 };
int dj[] = { 0, 0, -1, 1 };
char cc[N][N + 1];
int dd_[N][N], dd[N][N], qu[N * N], n, s;
int solve(int im, int jm, int id, int jd, int t) {
int i, j, head, cnt;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
dd[i][j] = n * n;
head = cnt = 0;
dd[im][jm] = 0, qu[head + cnt++] = im * n + jm;
while (cnt) {
int ij, h, d;
ij = qu[cnt--, head++], i = ij / n, j = ij % n, d = dd[i][j] + 1;
if (dd_[i][j] <= t + dd[i][j] / s)
continue;
if (i == id && j == jd)
return 1;
for (h = 0; h < 4; h++) {
int i_ = i + di[h], j_ = j + dj[h];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n && cc[i_][j_] != 'T' && dd[i_][j_] > d)
dd[i_][j_] = d, qu[head + cnt++] = i_ * n + j_;
}
}
return 0;
}
int main() {
int i, j, im, jm, id, jd, head, cnt, lower, upper;
scanf("%d%d", &n, &s);
for (i = 0; i < n; i++)
scanf("%s", cc[i]);
head = cnt = 0;
im = jm = id = jd = -1;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++) {
if (cc[i][j] == 'H')
dd_[i][j] = 0, qu[head + cnt++] = i * n + j;
else
dd_[i][j] = n * n;
if (cc[i][j] == 'M')
im = i, jm = j;
else if (cc[i][j] == 'D')
id = i, jd = j;
}
while (cnt) {
int ij, d, h;
ij = qu[cnt--, head++], i = ij / n, j = ij % n, d = dd_[i][j] + 1;
for (h = 0; h < 4; h++) {
int i_ = i + di[h], j_ = j + dj[h];
if (i_ >= 0 && i_ < n && j_ >= 0 && j_ < n && (cc[i_][j_] == 'G' || cc[i_][j_] == 'M') && dd_[i_][j_] > d)
dd_[i_][j_] = d, qu[head + cnt++] = i_ * n + j_;
}
}
lower = -1, upper = n * n + 1;
while (upper - lower > 1) {
int t = (lower + upper) / 2;
if (solve(im, jm, id, jd, t))
lower = t;
else
upper = t;
}
printf("%d\n", lower);
return 0;
}
Compilation message
mecho.c: In function 'main':
mecho.c:41:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | scanf("%d%d", &n, &s);
| ^~~~~~~~~~~~~~~~~~~~~
mecho.c:43:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%s", cc[i]);
| ^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
101 ms |
9024 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
672 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
844 KB |
Output is correct |
33 |
Correct |
5 ms |
3108 KB |
Output is correct |
34 |
Correct |
6 ms |
3020 KB |
Output is correct |
35 |
Correct |
24 ms |
3208 KB |
Output is correct |
36 |
Correct |
7 ms |
3532 KB |
Output is correct |
37 |
Correct |
7 ms |
3496 KB |
Output is correct |
38 |
Correct |
30 ms |
3628 KB |
Output is correct |
39 |
Correct |
8 ms |
4060 KB |
Output is correct |
40 |
Correct |
8 ms |
4044 KB |
Output is correct |
41 |
Correct |
42 ms |
4172 KB |
Output is correct |
42 |
Correct |
9 ms |
4520 KB |
Output is correct |
43 |
Correct |
10 ms |
4548 KB |
Output is correct |
44 |
Correct |
53 ms |
4784 KB |
Output is correct |
45 |
Correct |
11 ms |
5064 KB |
Output is correct |
46 |
Correct |
11 ms |
5052 KB |
Output is correct |
47 |
Correct |
59 ms |
5296 KB |
Output is correct |
48 |
Correct |
12 ms |
5580 KB |
Output is correct |
49 |
Correct |
12 ms |
5580 KB |
Output is correct |
50 |
Correct |
74 ms |
5908 KB |
Output is correct |
51 |
Correct |
15 ms |
6000 KB |
Output is correct |
52 |
Correct |
19 ms |
6092 KB |
Output is correct |
53 |
Correct |
110 ms |
6468 KB |
Output is correct |
54 |
Correct |
16 ms |
6572 KB |
Output is correct |
55 |
Correct |
16 ms |
6660 KB |
Output is correct |
56 |
Correct |
104 ms |
7000 KB |
Output is correct |
57 |
Correct |
23 ms |
7208 KB |
Output is correct |
58 |
Correct |
21 ms |
7172 KB |
Output is correct |
59 |
Correct |
118 ms |
7764 KB |
Output is correct |
60 |
Correct |
21 ms |
7668 KB |
Output is correct |
61 |
Correct |
22 ms |
7756 KB |
Output is correct |
62 |
Correct |
136 ms |
8500 KB |
Output is correct |
63 |
Correct |
119 ms |
8132 KB |
Output is correct |
64 |
Correct |
229 ms |
8024 KB |
Output is correct |
65 |
Correct |
169 ms |
8016 KB |
Output is correct |
66 |
Correct |
137 ms |
8004 KB |
Output is correct |
67 |
Correct |
133 ms |
7900 KB |
Output is correct |
68 |
Correct |
55 ms |
7976 KB |
Output is correct |
69 |
Correct |
55 ms |
8004 KB |
Output is correct |
70 |
Correct |
42 ms |
8132 KB |
Output is correct |
71 |
Correct |
49 ms |
8028 KB |
Output is correct |
72 |
Correct |
39 ms |
8004 KB |
Output is correct |
73 |
Correct |
41 ms |
8968 KB |
Output is correct |
74 |
Correct |
79 ms |
8908 KB |
Output is correct |
75 |
Correct |
89 ms |
9008 KB |
Output is correct |
76 |
Correct |
77 ms |
9000 KB |
Output is correct |
77 |
Correct |
80 ms |
8900 KB |
Output is correct |
78 |
Correct |
99 ms |
8952 KB |
Output is correct |
79 |
Correct |
61 ms |
8996 KB |
Output is correct |
80 |
Correct |
82 ms |
9012 KB |
Output is correct |
81 |
Correct |
87 ms |
8896 KB |
Output is correct |
82 |
Correct |
81 ms |
9020 KB |
Output is correct |
83 |
Correct |
93 ms |
9000 KB |
Output is correct |
84 |
Correct |
80 ms |
8996 KB |
Output is correct |
85 |
Correct |
91 ms |
8896 KB |
Output is correct |
86 |
Correct |
88 ms |
8932 KB |
Output is correct |
87 |
Correct |
114 ms |
8912 KB |
Output is correct |
88 |
Correct |
97 ms |
9008 KB |
Output is correct |
89 |
Correct |
98 ms |
9008 KB |
Output is correct |
90 |
Correct |
101 ms |
9008 KB |
Output is correct |
91 |
Correct |
98 ms |
9008 KB |
Output is correct |
92 |
Correct |
123 ms |
8952 KB |
Output is correct |