#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s;
string a[900];
int x[] = {1, -1, 0, 0};
int y[] = {0, 0, 1, -1};
int dis1[900][900], dis2[900][900];
queue<pair<int,int>> Q, Q1;
pair<int,int> st, en;
vector<pair<int,int>> bees;
bool bfs(int mid)
{
for(int i = 0; i < 900; i++)
for(int j = 0; j < 900; j++)
dis1[i][j] = dis2[i][j] = (int)1e18;
while(!Q.empty()) Q.pop();
while(!Q1.empty()) Q1.pop();
for(auto u : bees) Q.push(u), dis2[u.first][u.second]=0;
Q1.push(st); dis1[st.first][st.second] = 0;
while(!Q.empty())
{
auto cur = Q.front();
int i = cur.first, j = cur.second;
if(dis2[i][j]==mid) break; Q.pop();
for(int k = 0; k < 4; k++)
{
int I = i+x[k], J = j+y[k];
if(I<0 or J<0 or I>=n or J>=n) continue;
if(a[I][J]!='G') continue;
if(dis2[I][J]==(int)1e18)
Q.push({I,J}), dis2[I][J] = dis2[i][j]+1;
}
}
int last = 0, xx = mid;
while(!Q1.empty())
{
xx++;
while(!Q1.empty())
{
auto cur = Q1.front();
int i = cur.first, j = cur.second;
if(dis1[i][j]-last==s) break; Q1.pop();
if(dis2[i][j]!=(int)1e18) continue;
for(int k = 0; k < 4; k++)
{
int I = i+x[k], J = j+y[k];
if(I<0 or J<0 or I>=n or J>=n) continue;
if(dis2[I][J]!=(int)1e18) continue;
if(a[I][J]=='D' and dis1[I][J]==(int)1e18) return true;
if(a[I][J]!='G') continue;
if(dis1[I][J]==(int)1e18)
Q1.push({I,J}), dis1[I][J] = dis1[i][j]+1;
}
}
while(!Q.empty())
{
auto cur = Q.front();
int i = cur.first, j = cur.second;
if(dis2[i][j]==xx) break; Q.pop();
for(int k = 0; k < 4; k++)
{
int I = i+x[k], J = j+y[k];
if(I<0 or J<0 or I>=n or J>=n) continue;
if(a[I][J]!='G') continue;
if(dis2[I][J]==(int)1e18)
Q.push({I,J}), dis2[I][J] = dis2[i][j]+1;
}
}
last+=s;
}
return false;
}
int32_t main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++)
{
cin >> a[i];
for(int j = 0; j < n; j++)
{
if(a[i][j]=='M') st = {i,j}, a[i][j] = 'G';
else if(a[i][j]=='D') en = {i,j};
else if(a[i][j]=='H') bees.push_back({i,j});
}
}
int l = -1, r = 1e9;
while(l<r)
{
int mid = (l+r+1)/2; if(mid<0) break;
if(bfs(mid)) l = mid;
else r = mid-1;
}
cout << l;
}
Compilation message
mecho.cpp: In function 'bool bfs(long long int)':
mecho.cpp:25:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
25 | if(dis2[i][j]==mid) break; Q.pop();
| ^~
mecho.cpp:25:36: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
25 | if(dis2[i][j]==mid) break; Q.pop();
| ^
mecho.cpp:43:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
43 | if(dis1[i][j]-last==s) break; Q1.pop();
| ^~
mecho.cpp:43:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
43 | if(dis1[i][j]-last==s) break; Q1.pop();
| ^~
mecho.cpp:60:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
60 | if(dis2[i][j]==xx) break; Q.pop();
| ^~
mecho.cpp:60:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
60 | if(dis2[i][j]==xx) break; Q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
13012 KB |
Output is correct |
2 |
Correct |
22 ms |
13028 KB |
Output is correct |
3 |
Correct |
23 ms |
13020 KB |
Output is correct |
4 |
Correct |
22 ms |
13028 KB |
Output is correct |
5 |
Correct |
24 ms |
13064 KB |
Output is correct |
6 |
Correct |
22 ms |
13028 KB |
Output is correct |
7 |
Correct |
392 ms |
13924 KB |
Output is correct |
8 |
Correct |
21 ms |
13032 KB |
Output is correct |
9 |
Correct |
24 ms |
13012 KB |
Output is correct |
10 |
Correct |
21 ms |
13012 KB |
Output is correct |
11 |
Correct |
22 ms |
13012 KB |
Output is correct |
12 |
Correct |
23 ms |
13040 KB |
Output is correct |
13 |
Correct |
24 ms |
12944 KB |
Output is correct |
14 |
Correct |
30 ms |
13044 KB |
Output is correct |
15 |
Correct |
24 ms |
13032 KB |
Output is correct |
16 |
Correct |
22 ms |
13012 KB |
Output is correct |
17 |
Correct |
22 ms |
13012 KB |
Output is correct |
18 |
Correct |
22 ms |
13036 KB |
Output is correct |
19 |
Correct |
22 ms |
13036 KB |
Output is correct |
20 |
Correct |
23 ms |
13040 KB |
Output is correct |
21 |
Correct |
24 ms |
12944 KB |
Output is correct |
22 |
Correct |
23 ms |
13012 KB |
Output is correct |
23 |
Correct |
22 ms |
13012 KB |
Output is correct |
24 |
Correct |
24 ms |
13036 KB |
Output is correct |
25 |
Correct |
31 ms |
13084 KB |
Output is correct |
26 |
Correct |
23 ms |
13044 KB |
Output is correct |
27 |
Correct |
23 ms |
13040 KB |
Output is correct |
28 |
Correct |
23 ms |
13044 KB |
Output is correct |
29 |
Correct |
23 ms |
13012 KB |
Output is correct |
30 |
Correct |
25 ms |
13000 KB |
Output is correct |
31 |
Correct |
25 ms |
12940 KB |
Output is correct |
32 |
Correct |
25 ms |
13040 KB |
Output is correct |
33 |
Correct |
66 ms |
13144 KB |
Output is correct |
34 |
Correct |
48 ms |
13140 KB |
Output is correct |
35 |
Correct |
65 ms |
13148 KB |
Output is correct |
36 |
Correct |
71 ms |
13188 KB |
Output is correct |
37 |
Correct |
57 ms |
13140 KB |
Output is correct |
38 |
Correct |
89 ms |
13192 KB |
Output is correct |
39 |
Correct |
79 ms |
13140 KB |
Output is correct |
40 |
Correct |
67 ms |
13228 KB |
Output is correct |
41 |
Correct |
131 ms |
13236 KB |
Output is correct |
42 |
Correct |
98 ms |
13200 KB |
Output is correct |
43 |
Correct |
98 ms |
13280 KB |
Output is correct |
44 |
Correct |
111 ms |
13300 KB |
Output is correct |
45 |
Correct |
106 ms |
13328 KB |
Output is correct |
46 |
Correct |
95 ms |
13328 KB |
Output is correct |
47 |
Correct |
174 ms |
13360 KB |
Output is correct |
48 |
Correct |
122 ms |
13404 KB |
Output is correct |
49 |
Correct |
118 ms |
13404 KB |
Output is correct |
50 |
Correct |
156 ms |
13412 KB |
Output is correct |
51 |
Correct |
140 ms |
13396 KB |
Output is correct |
52 |
Correct |
111 ms |
13396 KB |
Output is correct |
53 |
Correct |
171 ms |
13476 KB |
Output is correct |
54 |
Correct |
158 ms |
13536 KB |
Output is correct |
55 |
Correct |
125 ms |
13532 KB |
Output is correct |
56 |
Correct |
202 ms |
13560 KB |
Output is correct |
57 |
Correct |
182 ms |
13616 KB |
Output is correct |
58 |
Correct |
155 ms |
13608 KB |
Output is correct |
59 |
Correct |
223 ms |
13628 KB |
Output is correct |
60 |
Correct |
198 ms |
13652 KB |
Output is correct |
61 |
Correct |
158 ms |
13652 KB |
Output is correct |
62 |
Correct |
295 ms |
13704 KB |
Output is correct |
63 |
Correct |
388 ms |
13712 KB |
Output is correct |
64 |
Correct |
394 ms |
13700 KB |
Output is correct |
65 |
Correct |
411 ms |
13700 KB |
Output is correct |
66 |
Correct |
437 ms |
13700 KB |
Output is correct |
67 |
Correct |
389 ms |
13708 KB |
Output is correct |
68 |
Correct |
431 ms |
13760 KB |
Output is correct |
69 |
Correct |
427 ms |
13756 KB |
Output is correct |
70 |
Correct |
396 ms |
13652 KB |
Output is correct |
71 |
Correct |
461 ms |
13768 KB |
Output is correct |
72 |
Correct |
472 ms |
13760 KB |
Output is correct |
73 |
Correct |
353 ms |
14420 KB |
Output is correct |
74 |
Correct |
414 ms |
14272 KB |
Output is correct |
75 |
Correct |
392 ms |
14308 KB |
Output is correct |
76 |
Correct |
402 ms |
14292 KB |
Output is correct |
77 |
Correct |
395 ms |
14420 KB |
Output is correct |
78 |
Correct |
389 ms |
14236 KB |
Output is correct |
79 |
Correct |
413 ms |
14224 KB |
Output is correct |
80 |
Correct |
393 ms |
14164 KB |
Output is correct |
81 |
Correct |
397 ms |
14284 KB |
Output is correct |
82 |
Correct |
414 ms |
14216 KB |
Output is correct |
83 |
Correct |
419 ms |
14136 KB |
Output is correct |
84 |
Correct |
397 ms |
14136 KB |
Output is correct |
85 |
Correct |
392 ms |
14132 KB |
Output is correct |
86 |
Correct |
396 ms |
14140 KB |
Output is correct |
87 |
Correct |
405 ms |
14140 KB |
Output is correct |
88 |
Correct |
419 ms |
14036 KB |
Output is correct |
89 |
Correct |
469 ms |
14036 KB |
Output is correct |
90 |
Correct |
391 ms |
14036 KB |
Output is correct |
91 |
Correct |
402 ms |
14036 KB |
Output is correct |
92 |
Correct |
427 ms |
14156 KB |
Output is correct |