#include <bits/stdc++.h>
using namespace std;
const int Nmax = 808, inf = Nmax * Nmax;
int n, S;
char a[Nmax][Nmax];
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int tmp[Nmax][Nmax], D[Nmax][Nmax];
queue< pair<int,int> > q;
void prec()
{
int i, j, x, y, X, Y;
for(i=1; i<=n; ++i)
for(j=1; j<=n; ++j)
tmp[i][j] = inf;
for(i=1; i<=n; ++i)
for(j=1; j<=n; ++j)
if(a[i][j] == 'H')
{
q.push({i, j});
tmp[i][j] = 0;
}
while(q.size())
{
tie(x, y) = q.front();
q.pop();
for(i=0; i<4; ++i)
{
X = x + dx[i];
Y = y + dy[i];
if((a[X][Y] == 'G' || a[X][Y] == 'M') && tmp[X][Y] == inf)
{
tmp[X][Y] = tmp[x][y] + 1;
q.push({X, Y});
}
}
}
}
bool check(int T)
{
while(q.size()) q.pop();
int i, j, x, y, X, Y;
for(i=1; i<=n; ++i)
for(j=1; j<=n; ++j)
{
if(a[i][j] == 'M')
x = i, y = j;
D[i][j] = inf;
}
if(!(T < tmp[x][y])) return 0;
q.push({x, y});
D[x][y] = 0;
while(q.size())
{
tie(X, Y) = q.front();
q.pop();
for(i=0; i<4; ++i)
{
x = X + dx[i];
y = Y + dy[i];
if(a[x][y] == 'D') return 1;
if(a[x][y] == 'G' && D[X][Y] + 1 < S * (tmp[x][y] - T) && D[x][y] == inf)
{
D[x][y] = D[X][Y] + 1;
q.push({x, y});
}
}
}
return 0;
}
int main()
{
// freopen("input", "r", stdin);
cin.sync_with_stdio(false);
cin >> n >> S;
int i;
for(i=1; i<=n; ++i) cin >> (a[i] + 1);
prec();
int st, dr, mid;
st = 0; dr = n * n;
while(st <= dr)
{
mid = (st + dr) / 2;
if(check(mid)) st = mid + 1;
else dr = mid - 1;
}
cout << dr << '\n';
return 0;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:64:22: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
if(!(T < tmp[x][y])) return 0;
~~~~~~~~^
mecho.cpp:64:22: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
184 ms |
6776 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
768 KB |
Output is correct |
13 |
Correct |
2 ms |
768 KB |
Output is correct |
14 |
Correct |
2 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
896 KB |
Output is correct |
28 |
Correct |
3 ms |
768 KB |
Output is correct |
29 |
Correct |
2 ms |
896 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
1024 KB |
Output is correct |
32 |
Correct |
4 ms |
896 KB |
Output is correct |
33 |
Correct |
8 ms |
2816 KB |
Output is correct |
34 |
Correct |
8 ms |
2876 KB |
Output is correct |
35 |
Correct |
27 ms |
2944 KB |
Output is correct |
36 |
Correct |
9 ms |
3200 KB |
Output is correct |
37 |
Correct |
11 ms |
3328 KB |
Output is correct |
38 |
Correct |
32 ms |
3328 KB |
Output is correct |
39 |
Correct |
10 ms |
3584 KB |
Output is correct |
40 |
Correct |
10 ms |
3712 KB |
Output is correct |
41 |
Correct |
52 ms |
3816 KB |
Output is correct |
42 |
Correct |
12 ms |
3968 KB |
Output is correct |
43 |
Correct |
11 ms |
4224 KB |
Output is correct |
44 |
Correct |
65 ms |
4096 KB |
Output is correct |
45 |
Correct |
15 ms |
4224 KB |
Output is correct |
46 |
Correct |
15 ms |
4608 KB |
Output is correct |
47 |
Correct |
72 ms |
4608 KB |
Output is correct |
48 |
Correct |
14 ms |
4608 KB |
Output is correct |
49 |
Correct |
16 ms |
4992 KB |
Output is correct |
50 |
Correct |
75 ms |
4992 KB |
Output is correct |
51 |
Correct |
16 ms |
4992 KB |
Output is correct |
52 |
Correct |
18 ms |
5504 KB |
Output is correct |
53 |
Correct |
93 ms |
5376 KB |
Output is correct |
54 |
Correct |
20 ms |
5376 KB |
Output is correct |
55 |
Correct |
21 ms |
5760 KB |
Output is correct |
56 |
Correct |
135 ms |
5880 KB |
Output is correct |
57 |
Correct |
23 ms |
5760 KB |
Output is correct |
58 |
Correct |
23 ms |
6272 KB |
Output is correct |
59 |
Correct |
154 ms |
6264 KB |
Output is correct |
60 |
Correct |
34 ms |
6016 KB |
Output is correct |
61 |
Correct |
26 ms |
6648 KB |
Output is correct |
62 |
Correct |
239 ms |
6648 KB |
Output is correct |
63 |
Correct |
132 ms |
6648 KB |
Output is correct |
64 |
Correct |
238 ms |
6732 KB |
Output is correct |
65 |
Correct |
197 ms |
6648 KB |
Output is correct |
66 |
Correct |
165 ms |
6776 KB |
Output is correct |
67 |
Correct |
164 ms |
6648 KB |
Output is correct |
68 |
Correct |
77 ms |
6648 KB |
Output is correct |
69 |
Correct |
72 ms |
6624 KB |
Output is correct |
70 |
Correct |
54 ms |
6648 KB |
Output is correct |
71 |
Correct |
56 ms |
6648 KB |
Output is correct |
72 |
Correct |
48 ms |
6648 KB |
Output is correct |
73 |
Correct |
67 ms |
6904 KB |
Output is correct |
74 |
Correct |
79 ms |
6904 KB |
Output is correct |
75 |
Correct |
99 ms |
7004 KB |
Output is correct |
76 |
Correct |
90 ms |
6904 KB |
Output is correct |
77 |
Correct |
105 ms |
6876 KB |
Output is correct |
78 |
Correct |
105 ms |
6912 KB |
Output is correct |
79 |
Correct |
65 ms |
6892 KB |
Output is correct |
80 |
Correct |
106 ms |
6904 KB |
Output is correct |
81 |
Correct |
119 ms |
6912 KB |
Output is correct |
82 |
Correct |
115 ms |
6904 KB |
Output is correct |
83 |
Correct |
135 ms |
6912 KB |
Output is correct |
84 |
Correct |
123 ms |
6884 KB |
Output is correct |
85 |
Correct |
116 ms |
7032 KB |
Output is correct |
86 |
Correct |
112 ms |
6904 KB |
Output is correct |
87 |
Correct |
97 ms |
6912 KB |
Output is correct |
88 |
Correct |
125 ms |
6868 KB |
Output is correct |
89 |
Correct |
145 ms |
6892 KB |
Output is correct |
90 |
Correct |
118 ms |
6864 KB |
Output is correct |
91 |
Correct |
111 ms |
6872 KB |
Output is correct |
92 |
Correct |
110 ms |
6776 KB |
Output is correct |