#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 |
34 ms |
13008 KB |
Output is correct |
2 |
Correct |
37 ms |
13004 KB |
Output is correct |
3 |
Correct |
36 ms |
13040 KB |
Output is correct |
4 |
Correct |
41 ms |
13004 KB |
Output is correct |
5 |
Correct |
43 ms |
13024 KB |
Output is correct |
6 |
Correct |
36 ms |
13004 KB |
Output is correct |
7 |
Correct |
469 ms |
14540 KB |
Output is correct |
8 |
Correct |
42 ms |
13124 KB |
Output is correct |
9 |
Correct |
40 ms |
13004 KB |
Output is correct |
10 |
Correct |
37 ms |
13024 KB |
Output is correct |
11 |
Correct |
36 ms |
13004 KB |
Output is correct |
12 |
Correct |
37 ms |
13004 KB |
Output is correct |
13 |
Correct |
39 ms |
13004 KB |
Output is correct |
14 |
Correct |
40 ms |
13004 KB |
Output is correct |
15 |
Correct |
41 ms |
13124 KB |
Output is correct |
16 |
Correct |
38 ms |
13020 KB |
Output is correct |
17 |
Correct |
38 ms |
12888 KB |
Output is correct |
18 |
Correct |
37 ms |
13132 KB |
Output is correct |
19 |
Correct |
36 ms |
13004 KB |
Output is correct |
20 |
Correct |
38 ms |
13004 KB |
Output is correct |
21 |
Correct |
37 ms |
13028 KB |
Output is correct |
22 |
Correct |
38 ms |
13008 KB |
Output is correct |
23 |
Correct |
37 ms |
13004 KB |
Output is correct |
24 |
Correct |
38 ms |
13004 KB |
Output is correct |
25 |
Correct |
35 ms |
13024 KB |
Output is correct |
26 |
Correct |
38 ms |
13004 KB |
Output is correct |
27 |
Correct |
37 ms |
13004 KB |
Output is correct |
28 |
Correct |
37 ms |
13004 KB |
Output is correct |
29 |
Correct |
39 ms |
13028 KB |
Output is correct |
30 |
Correct |
38 ms |
13004 KB |
Output is correct |
31 |
Correct |
39 ms |
13024 KB |
Output is correct |
32 |
Correct |
38 ms |
13020 KB |
Output is correct |
33 |
Correct |
80 ms |
13252 KB |
Output is correct |
34 |
Correct |
65 ms |
13252 KB |
Output is correct |
35 |
Correct |
99 ms |
13264 KB |
Output is correct |
36 |
Correct |
91 ms |
13268 KB |
Output is correct |
37 |
Correct |
80 ms |
13280 KB |
Output is correct |
38 |
Correct |
123 ms |
13268 KB |
Output is correct |
39 |
Correct |
100 ms |
13388 KB |
Output is correct |
40 |
Correct |
90 ms |
13424 KB |
Output is correct |
41 |
Correct |
135 ms |
13428 KB |
Output is correct |
42 |
Correct |
118 ms |
13508 KB |
Output is correct |
43 |
Correct |
96 ms |
13516 KB |
Output is correct |
44 |
Correct |
165 ms |
13416 KB |
Output is correct |
45 |
Correct |
135 ms |
13544 KB |
Output is correct |
46 |
Correct |
109 ms |
13624 KB |
Output is correct |
47 |
Correct |
199 ms |
13644 KB |
Output is correct |
48 |
Correct |
147 ms |
13748 KB |
Output is correct |
49 |
Correct |
124 ms |
13672 KB |
Output is correct |
50 |
Correct |
220 ms |
13760 KB |
Output is correct |
51 |
Correct |
168 ms |
13872 KB |
Output is correct |
52 |
Correct |
137 ms |
13876 KB |
Output is correct |
53 |
Correct |
264 ms |
13880 KB |
Output is correct |
54 |
Correct |
198 ms |
14008 KB |
Output is correct |
55 |
Correct |
157 ms |
14004 KB |
Output is correct |
56 |
Correct |
292 ms |
14148 KB |
Output is correct |
57 |
Correct |
215 ms |
14160 KB |
Output is correct |
58 |
Correct |
163 ms |
14152 KB |
Output is correct |
59 |
Correct |
329 ms |
14164 KB |
Output is correct |
60 |
Correct |
244 ms |
14284 KB |
Output is correct |
61 |
Correct |
205 ms |
14308 KB |
Output is correct |
62 |
Correct |
389 ms |
14324 KB |
Output is correct |
63 |
Correct |
485 ms |
14320 KB |
Output is correct |
64 |
Correct |
513 ms |
14296 KB |
Output is correct |
65 |
Correct |
515 ms |
14404 KB |
Output is correct |
66 |
Correct |
513 ms |
14272 KB |
Output is correct |
67 |
Correct |
488 ms |
14284 KB |
Output is correct |
68 |
Correct |
562 ms |
14384 KB |
Output is correct |
69 |
Correct |
500 ms |
14276 KB |
Output is correct |
70 |
Correct |
503 ms |
14376 KB |
Output is correct |
71 |
Correct |
515 ms |
14328 KB |
Output is correct |
72 |
Correct |
606 ms |
14276 KB |
Output is correct |
73 |
Correct |
463 ms |
15048 KB |
Output is correct |
74 |
Correct |
530 ms |
14960 KB |
Output is correct |
75 |
Correct |
564 ms |
14900 KB |
Output is correct |
76 |
Correct |
523 ms |
14904 KB |
Output is correct |
77 |
Correct |
565 ms |
15112 KB |
Output is correct |
78 |
Correct |
493 ms |
14836 KB |
Output is correct |
79 |
Correct |
518 ms |
14928 KB |
Output is correct |
80 |
Correct |
517 ms |
14840 KB |
Output is correct |
81 |
Correct |
535 ms |
14840 KB |
Output is correct |
82 |
Correct |
537 ms |
14908 KB |
Output is correct |
83 |
Correct |
504 ms |
14660 KB |
Output is correct |
84 |
Correct |
516 ms |
14888 KB |
Output is correct |
85 |
Correct |
532 ms |
14772 KB |
Output is correct |
86 |
Correct |
520 ms |
14760 KB |
Output is correct |
87 |
Correct |
499 ms |
14660 KB |
Output is correct |
88 |
Correct |
503 ms |
14644 KB |
Output is correct |
89 |
Correct |
524 ms |
14648 KB |
Output is correct |
90 |
Correct |
504 ms |
14648 KB |
Output is correct |
91 |
Correct |
504 ms |
14776 KB |
Output is correct |
92 |
Correct |
514 ms |
14648 KB |
Output is correct |