#include <bits/stdc++.h>
using namespace std;
vector<int> adj[640005], bee;
queue<pair<int, int> > qq;
char a[640005];
bool b[640005];
int d[640005];
queue<int> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, p, s, e, l, r;
cin >> n >> p;
for (int i=0; i<n*n; i++)
{
cin >> a[i];
if (a[i]=='M')
s=i;
else if (a[i]=='D')
e=i;
else if (a[i]=='H')
bee.push_back(i);
if (a[i]!='T')
{
a[i]='G';
if (i>=n && a[i-n]=='G')
{
adj[i].push_back(i-n);
adj[i-n].push_back(i);
}
if (i%n && a[i-1]=='G')
{
adj[i].push_back(i-1);
adj[i-1].push_back(i);
}
}
}
for (int u:bee)
{
b[u]=1;
q.push(u);
}
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v:adj[u])
{
if (!b[v] && v!=e)
{
d[v]=d[u]+1;
b[v]=1;
q.push(v);
}
}
}
l=-1;
r=d[s]-1;
while (l<r)
{
int m=(l+r+1)/2;
for (int i=0; i<n*n; i++)
b[i]=0;
qq.push({s, 0});
b[s]=1;
bool ok=0;
while (!qq.empty())
{
int u=qq.front().first, w=qq.front().second;
qq.pop();
for (int v:adj[u])
{
if (v==e)
{
ok=1;
break;
}
if (!b[v] && w+1<(d[v]-m)*p)
{
b[v]=1;
qq.push({v, w+1});
}
}
if (ok)
while (!qq.empty())
qq.pop();
}
if (ok)
l=m;
else
r=m-1;
}
cout << l;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:60:10: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | r=d[s]-1;
| ~~~^
mecho.cpp:51:23: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
51 | if (!b[v] && v!=e)
| ~~~~~~^~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
8 ms |
15324 KB |
Output is correct |
3 |
Correct |
10 ms |
15316 KB |
Output is correct |
4 |
Correct |
10 ms |
15316 KB |
Output is correct |
5 |
Correct |
9 ms |
15316 KB |
Output is correct |
6 |
Correct |
10 ms |
15284 KB |
Output is correct |
7 |
Correct |
345 ms |
39116 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
11 ms |
15444 KB |
Output is correct |
10 |
Correct |
8 ms |
15316 KB |
Output is correct |
11 |
Correct |
9 ms |
15368 KB |
Output is correct |
12 |
Correct |
8 ms |
15444 KB |
Output is correct |
13 |
Correct |
9 ms |
15444 KB |
Output is correct |
14 |
Correct |
8 ms |
15444 KB |
Output is correct |
15 |
Correct |
9 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15316 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
9 ms |
15316 KB |
Output is correct |
19 |
Correct |
11 ms |
15316 KB |
Output is correct |
20 |
Correct |
9 ms |
15316 KB |
Output is correct |
21 |
Correct |
8 ms |
15316 KB |
Output is correct |
22 |
Correct |
9 ms |
15316 KB |
Output is correct |
23 |
Correct |
9 ms |
15316 KB |
Output is correct |
24 |
Correct |
8 ms |
15316 KB |
Output is correct |
25 |
Correct |
8 ms |
15396 KB |
Output is correct |
26 |
Correct |
8 ms |
15444 KB |
Output is correct |
27 |
Correct |
10 ms |
15348 KB |
Output is correct |
28 |
Correct |
9 ms |
15400 KB |
Output is correct |
29 |
Correct |
8 ms |
15444 KB |
Output is correct |
30 |
Correct |
8 ms |
15444 KB |
Output is correct |
31 |
Correct |
9 ms |
15444 KB |
Output is correct |
32 |
Correct |
9 ms |
15496 KB |
Output is correct |
33 |
Correct |
19 ms |
18004 KB |
Output is correct |
34 |
Correct |
19 ms |
18020 KB |
Output is correct |
35 |
Correct |
60 ms |
18900 KB |
Output is correct |
36 |
Correct |
18 ms |
18712 KB |
Output is correct |
37 |
Correct |
20 ms |
18764 KB |
Output is correct |
38 |
Correct |
69 ms |
20060 KB |
Output is correct |
39 |
Correct |
21 ms |
19668 KB |
Output is correct |
40 |
Correct |
23 ms |
19640 KB |
Output is correct |
41 |
Correct |
95 ms |
21292 KB |
Output is correct |
42 |
Correct |
26 ms |
20812 KB |
Output is correct |
43 |
Correct |
31 ms |
20692 KB |
Output is correct |
44 |
Correct |
131 ms |
22664 KB |
Output is correct |
45 |
Correct |
28 ms |
21868 KB |
Output is correct |
46 |
Correct |
29 ms |
21848 KB |
Output is correct |
47 |
Correct |
155 ms |
24140 KB |
Output is correct |
48 |
Correct |
30 ms |
23116 KB |
Output is correct |
49 |
Correct |
31 ms |
23004 KB |
Output is correct |
50 |
Correct |
168 ms |
25812 KB |
Output is correct |
51 |
Correct |
37 ms |
24344 KB |
Output is correct |
52 |
Correct |
44 ms |
24444 KB |
Output is correct |
53 |
Correct |
256 ms |
27664 KB |
Output is correct |
54 |
Correct |
38 ms |
25792 KB |
Output is correct |
55 |
Correct |
40 ms |
25804 KB |
Output is correct |
56 |
Correct |
281 ms |
29648 KB |
Output is correct |
57 |
Correct |
43 ms |
27468 KB |
Output is correct |
58 |
Correct |
43 ms |
27340 KB |
Output is correct |
59 |
Correct |
336 ms |
31836 KB |
Output is correct |
60 |
Correct |
48 ms |
29132 KB |
Output is correct |
61 |
Correct |
50 ms |
29060 KB |
Output is correct |
62 |
Correct |
384 ms |
34096 KB |
Output is correct |
63 |
Correct |
212 ms |
31052 KB |
Output is correct |
64 |
Correct |
319 ms |
31048 KB |
Output is correct |
65 |
Correct |
328 ms |
31052 KB |
Output is correct |
66 |
Correct |
249 ms |
31056 KB |
Output is correct |
67 |
Correct |
238 ms |
31052 KB |
Output is correct |
68 |
Correct |
136 ms |
31132 KB |
Output is correct |
69 |
Correct |
127 ms |
31072 KB |
Output is correct |
70 |
Correct |
121 ms |
31080 KB |
Output is correct |
71 |
Correct |
101 ms |
30976 KB |
Output is correct |
72 |
Correct |
107 ms |
31080 KB |
Output is correct |
73 |
Correct |
218 ms |
39092 KB |
Output is correct |
74 |
Correct |
228 ms |
39092 KB |
Output is correct |
75 |
Correct |
229 ms |
39096 KB |
Output is correct |
76 |
Correct |
244 ms |
39096 KB |
Output is correct |
77 |
Correct |
226 ms |
39132 KB |
Output is correct |
78 |
Correct |
242 ms |
38992 KB |
Output is correct |
79 |
Correct |
203 ms |
39080 KB |
Output is correct |
80 |
Correct |
241 ms |
39116 KB |
Output is correct |
81 |
Correct |
228 ms |
39080 KB |
Output is correct |
82 |
Correct |
210 ms |
39060 KB |
Output is correct |
83 |
Correct |
332 ms |
38928 KB |
Output is correct |
84 |
Correct |
260 ms |
39020 KB |
Output is correct |
85 |
Correct |
293 ms |
39008 KB |
Output is correct |
86 |
Correct |
304 ms |
39020 KB |
Output is correct |
87 |
Correct |
280 ms |
39080 KB |
Output is correct |
88 |
Correct |
334 ms |
38928 KB |
Output is correct |
89 |
Correct |
316 ms |
39036 KB |
Output is correct |
90 |
Correct |
332 ms |
39040 KB |
Output is correct |
91 |
Correct |
317 ms |
39036 KB |
Output is correct |
92 |
Correct |
346 ms |
39036 KB |
Output is correct |