#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=-1, r=640000;
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)
{
q.push(u);
b[u]=1;
}
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v:adj[u])
{
if (!b[v])
{
d[v]=d[u]+1;
q.push(v);
b[v]=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 (!b[v] && w+1<(d[v]-m)*p)
{
if (v==e)
{
ok=1;
break;
}
qq.push({v, w+1});
b[v]=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:65:13: warning: 's' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | b[s]=1;
| ~~~~^~
mecho.cpp:75:21: warning: 'e' may be used uninitialized in this function [-Wmaybe-uninitialized]
75 | if (v==e)
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15360 KB |
Output is correct |
2 |
Incorrect |
10 ms |
15316 KB |
Output isn't correct |
3 |
Correct |
9 ms |
15268 KB |
Output is correct |
4 |
Incorrect |
10 ms |
15316 KB |
Output isn't correct |
5 |
Correct |
8 ms |
15368 KB |
Output is correct |
6 |
Incorrect |
9 ms |
15368 KB |
Output isn't correct |
7 |
Incorrect |
333 ms |
39816 KB |
Output isn't correct |
8 |
Correct |
9 ms |
15360 KB |
Output is correct |
9 |
Correct |
8 ms |
15316 KB |
Output is correct |
10 |
Correct |
8 ms |
15316 KB |
Output is correct |
11 |
Correct |
8 ms |
15268 KB |
Output is correct |
12 |
Incorrect |
10 ms |
15416 KB |
Output isn't correct |
13 |
Incorrect |
9 ms |
15420 KB |
Output isn't correct |
14 |
Correct |
9 ms |
15444 KB |
Output is correct |
15 |
Correct |
9 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15364 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
8 ms |
15316 KB |
Output is correct |
19 |
Correct |
8 ms |
15316 KB |
Output is correct |
20 |
Correct |
8 ms |
15356 KB |
Output is correct |
21 |
Correct |
8 ms |
15364 KB |
Output is correct |
22 |
Correct |
9 ms |
15368 KB |
Output is correct |
23 |
Correct |
10 ms |
15316 KB |
Output is correct |
24 |
Correct |
9 ms |
15316 KB |
Output is correct |
25 |
Correct |
8 ms |
15444 KB |
Output is correct |
26 |
Correct |
10 ms |
15460 KB |
Output is correct |
27 |
Correct |
9 ms |
15444 KB |
Output is correct |
28 |
Correct |
11 ms |
15424 KB |
Output is correct |
29 |
Correct |
9 ms |
15424 KB |
Output is correct |
30 |
Correct |
9 ms |
15476 KB |
Output is correct |
31 |
Correct |
9 ms |
15572 KB |
Output is correct |
32 |
Correct |
9 ms |
15444 KB |
Output is correct |
33 |
Correct |
16 ms |
18132 KB |
Output is correct |
34 |
Correct |
16 ms |
18068 KB |
Output is correct |
35 |
Correct |
56 ms |
18988 KB |
Output is correct |
36 |
Correct |
18 ms |
18900 KB |
Output is correct |
37 |
Correct |
21 ms |
18928 KB |
Output is correct |
38 |
Correct |
74 ms |
20200 KB |
Output is correct |
39 |
Correct |
21 ms |
19908 KB |
Output is correct |
40 |
Correct |
22 ms |
19924 KB |
Output is correct |
41 |
Correct |
94 ms |
21484 KB |
Output is correct |
42 |
Correct |
24 ms |
20948 KB |
Output is correct |
43 |
Correct |
23 ms |
21048 KB |
Output is correct |
44 |
Correct |
129 ms |
22868 KB |
Output is correct |
45 |
Correct |
28 ms |
22252 KB |
Output is correct |
46 |
Correct |
29 ms |
22224 KB |
Output is correct |
47 |
Correct |
167 ms |
24520 KB |
Output is correct |
48 |
Correct |
30 ms |
23568 KB |
Output is correct |
49 |
Correct |
32 ms |
23380 KB |
Output is correct |
50 |
Correct |
196 ms |
26256 KB |
Output is correct |
51 |
Correct |
33 ms |
24844 KB |
Output is correct |
52 |
Correct |
36 ms |
24904 KB |
Output is correct |
53 |
Correct |
258 ms |
28148 KB |
Output is correct |
54 |
Correct |
38 ms |
26320 KB |
Output is correct |
55 |
Correct |
48 ms |
26392 KB |
Output is correct |
56 |
Correct |
285 ms |
30288 KB |
Output is correct |
57 |
Correct |
42 ms |
27984 KB |
Output is correct |
58 |
Correct |
44 ms |
27908 KB |
Output is correct |
59 |
Correct |
343 ms |
32388 KB |
Output is correct |
60 |
Correct |
53 ms |
29712 KB |
Output is correct |
61 |
Correct |
51 ms |
29776 KB |
Output is correct |
62 |
Correct |
394 ms |
34696 KB |
Output is correct |
63 |
Correct |
211 ms |
31760 KB |
Output is correct |
64 |
Correct |
366 ms |
31632 KB |
Output is correct |
65 |
Correct |
288 ms |
31672 KB |
Output is correct |
66 |
Correct |
253 ms |
31688 KB |
Output is correct |
67 |
Correct |
235 ms |
31568 KB |
Output is correct |
68 |
Correct |
130 ms |
31612 KB |
Output is correct |
69 |
Correct |
132 ms |
31708 KB |
Output is correct |
70 |
Correct |
118 ms |
31700 KB |
Output is correct |
71 |
Correct |
109 ms |
31600 KB |
Output is correct |
72 |
Incorrect |
93 ms |
31588 KB |
Output isn't correct |
73 |
Incorrect |
192 ms |
39692 KB |
Output isn't correct |
74 |
Correct |
217 ms |
39620 KB |
Output is correct |
75 |
Correct |
259 ms |
39644 KB |
Output is correct |
76 |
Correct |
238 ms |
39664 KB |
Output is correct |
77 |
Correct |
219 ms |
39628 KB |
Output is correct |
78 |
Correct |
240 ms |
39596 KB |
Output is correct |
79 |
Correct |
181 ms |
39628 KB |
Output is correct |
80 |
Correct |
216 ms |
39812 KB |
Output is correct |
81 |
Correct |
222 ms |
39628 KB |
Output is correct |
82 |
Correct |
201 ms |
39720 KB |
Output is correct |
83 |
Correct |
313 ms |
39612 KB |
Output is correct |
84 |
Correct |
281 ms |
39584 KB |
Output is correct |
85 |
Correct |
307 ms |
39640 KB |
Output is correct |
86 |
Correct |
316 ms |
39644 KB |
Output is correct |
87 |
Correct |
288 ms |
39644 KB |
Output is correct |
88 |
Correct |
324 ms |
39660 KB |
Output is correct |
89 |
Correct |
311 ms |
39664 KB |
Output is correct |
90 |
Correct |
319 ms |
39660 KB |
Output is correct |
91 |
Correct |
337 ms |
39632 KB |
Output is correct |
92 |
Correct |
332 ms |
39696 KB |
Output is correct |