#include <bits/stdc++.h>
#define ll long long
#define pi pair<ll, ll>
#define fi first
#define se second
using namespace std;
const int u[] = {0, 1, 0, -1};
const int v[] = {1, 0, -1, 0};
int n, steps;
char grid[805][805];
pi init, home;
vector<pi> honey;
bool check(ll cur, ll dist, ll m)
{
if (cur % steps == 0)
{
return (cur / steps + m < dist);
} else
{
return (cur / steps + m + 1 <= dist);
}
}
bool safe(int u, int v)
{
return (u >= 0 && u < n && v >= 0 && v < n);
}
void solve()
{
cin >> n >> steps;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin >> grid[i][j];
if (grid[i][j] == 'M')
{
init = make_pair(i, j);
}
if (grid[i][j] == 'D')
{
home = make_pair(i, j);
}
if (grid[i][j] == 'H')
{
honey.push_back({i, j});
}
}
}
vector<vector<ll>> dist1(n, vector<ll>(n, -1));
queue<pi> que;
for(pi x : honey)
{
que.push(x);
dist1[x.fi][x.se] = 0;
}
while(!que.empty())
{
pi now = que.front(); que.pop();
int curU = now.fi, curV = now.se;
for(int i=0; i<4; i++)
{
int ux = u[i] + curU, vx = v[i] + curV;
if (safe(ux, vx) && (grid[ux][vx] == 'G' || grid[ux][vx] == 'M') && dist1[ux][vx] == -1)
{
dist1[ux][vx] = dist1[curU][curV] + 1;
que.push({ux, vx});
}
}
}
ll l = 0, r = 1e9;
ll ans = -1;
while(l <= r)
{
ll m = (l + r) >> 1;
que.push(init);
vector<vector<ll>> dis(n, vector<ll>(n, -1));
dis[init.fi][init.se] = 0;
if (m < dist1[init.fi][init.se])
{
while(!que.empty())
{
pi now = que.front(); que.pop();
int curU = now.fi, curV = now.se;
for(int i=0; i<4; i++)
{
int ux = curU + u[i], vx = curV + v[i];
if (ux == home.fi && vx == home.se)
{
dis[ux][vx] = dis[curU][curV] + 1;
break;
}
if (safe(ux, vx) && grid[ux][vx] != 'H' && dis[ux][vx] == -1 && check(dis[curU][curV] + 1, dist1[ux][vx], m))
{
dis[ux][vx] = dis[curU][curV] + 1;
que.push({ux, vx});
}
}
}
}
if (dis[home.fi][home.se] == -1)
{
r = m - 1;
} else
{
ans = m;
l = m + 1;
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
135 ms |
11288 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
13 ms |
2548 KB |
Output is correct |
34 |
Correct |
13 ms |
2548 KB |
Output is correct |
35 |
Correct |
28 ms |
2556 KB |
Output is correct |
36 |
Correct |
15 ms |
3176 KB |
Output is correct |
37 |
Correct |
15 ms |
3248 KB |
Output is correct |
38 |
Correct |
37 ms |
3268 KB |
Output is correct |
39 |
Correct |
19 ms |
3968 KB |
Output is correct |
40 |
Correct |
23 ms |
3960 KB |
Output is correct |
41 |
Correct |
50 ms |
3884 KB |
Output is correct |
42 |
Correct |
22 ms |
4756 KB |
Output is correct |
43 |
Correct |
23 ms |
4672 KB |
Output is correct |
44 |
Correct |
65 ms |
4740 KB |
Output is correct |
45 |
Correct |
30 ms |
5732 KB |
Output is correct |
46 |
Correct |
28 ms |
5628 KB |
Output is correct |
47 |
Correct |
82 ms |
5616 KB |
Output is correct |
48 |
Correct |
36 ms |
6564 KB |
Output is correct |
49 |
Correct |
40 ms |
6592 KB |
Output is correct |
50 |
Correct |
105 ms |
6584 KB |
Output is correct |
51 |
Correct |
43 ms |
7556 KB |
Output is correct |
52 |
Correct |
43 ms |
7668 KB |
Output is correct |
53 |
Correct |
124 ms |
7636 KB |
Output is correct |
54 |
Correct |
52 ms |
8656 KB |
Output is correct |
55 |
Correct |
52 ms |
8748 KB |
Output is correct |
56 |
Correct |
148 ms |
8736 KB |
Output is correct |
57 |
Correct |
57 ms |
9904 KB |
Output is correct |
58 |
Correct |
54 ms |
9884 KB |
Output is correct |
59 |
Correct |
172 ms |
9908 KB |
Output is correct |
60 |
Correct |
69 ms |
11228 KB |
Output is correct |
61 |
Correct |
65 ms |
11236 KB |
Output is correct |
62 |
Correct |
202 ms |
11152 KB |
Output is correct |
63 |
Correct |
165 ms |
11236 KB |
Output is correct |
64 |
Correct |
249 ms |
11180 KB |
Output is correct |
65 |
Correct |
244 ms |
11136 KB |
Output is correct |
66 |
Correct |
189 ms |
11200 KB |
Output is correct |
67 |
Correct |
167 ms |
11144 KB |
Output is correct |
68 |
Correct |
107 ms |
11228 KB |
Output is correct |
69 |
Correct |
100 ms |
11220 KB |
Output is correct |
70 |
Correct |
78 ms |
11184 KB |
Output is correct |
71 |
Correct |
83 ms |
11176 KB |
Output is correct |
72 |
Correct |
83 ms |
11200 KB |
Output is correct |
73 |
Correct |
90 ms |
11360 KB |
Output is correct |
74 |
Correct |
105 ms |
11256 KB |
Output is correct |
75 |
Correct |
113 ms |
11232 KB |
Output is correct |
76 |
Correct |
107 ms |
11232 KB |
Output is correct |
77 |
Correct |
119 ms |
11236 KB |
Output is correct |
78 |
Correct |
118 ms |
11264 KB |
Output is correct |
79 |
Correct |
102 ms |
11248 KB |
Output is correct |
80 |
Correct |
106 ms |
11284 KB |
Output is correct |
81 |
Correct |
112 ms |
11312 KB |
Output is correct |
82 |
Correct |
113 ms |
11336 KB |
Output is correct |
83 |
Correct |
130 ms |
11244 KB |
Output is correct |
84 |
Correct |
124 ms |
11224 KB |
Output is correct |
85 |
Correct |
114 ms |
11236 KB |
Output is correct |
86 |
Correct |
129 ms |
11244 KB |
Output is correct |
87 |
Correct |
126 ms |
11196 KB |
Output is correct |
88 |
Correct |
160 ms |
11260 KB |
Output is correct |
89 |
Correct |
130 ms |
11244 KB |
Output is correct |
90 |
Correct |
133 ms |
11232 KB |
Output is correct |
91 |
Correct |
131 ms |
11256 KB |
Output is correct |
92 |
Correct |
142 ms |
11276 KB |
Output is correct |