#include <bits/stdc++.h>
using namespace std;
int nx[4]={1, -1, 0, 0}, ny[4]={0, 0, 1, -1};
char grid[2000][2000]; bool vis[2000][2000];
int d[2000][2000]; int n, s, sx, sy, hx, hy;
bool bfs(int v)
{
if(v*s>=d[sx][sy]) return false;
memset(vis, 0, sizeof vis);
deque<pair<int, pair<int, int>>>q;
q.push_back(make_pair(v*s, make_pair(sx, sy)));
vis[sx][sy]=true;
while(!q.empty()) {
int dis=q.front().first;
int a=q.front().second.first, b=q.front().second.second;
q.pop_front();
if(grid[a][b]=='D') return true;
for(int l=0;l<4;l++) {
int x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<n) {
if(grid[x][y]!='T'&&dis+1<d[x][y]&&!vis[x][y]) {
q.push_back(make_pair(dis+1, make_pair(x, y)));
vis[x][y]=true;
}
}
}
}
return false;
}
int main()
{
cin>>n>>s;
deque<pair<int, int>>q;
memset(d, -1, sizeof(d));
for(int l=0;l<n;l++) {
for(int i=0;i<n;i++)
{
cin>>grid[l][i];
if(grid[l][i]=='H') {
q.push_back(make_pair(l, i));
d[l][i]=0;
}
else if(grid[l][i]=='M') {
sx=l; sy=i; grid[l][i]='G';
}
else if(grid[l][i]=='D') {
hx=l; hy=i;
}
}
}
while(!q.empty()) {
int a=q.front().first, b=q.front().second; q.pop_front();
for(int l=0;l<4;l++) {
int x=a+nx[l], y=b+ny[l];
if(x>=0&&x<n&&y>=0&&y<n&&grid[x][y]=='G'&&d[x][y]==-1) {
d[x][y]=d[a][b]+s; q.push_back(make_pair(x, y));
}
}
}
d[hx][hy]=n*n*s;
int lo=-1, hi=2*n*n;
while(hi-lo>1) {
int mid=(hi+lo)/2;
if(bfs(mid)) lo=mid;
else hi=mid;
}
cout<<lo<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19788 KB |
Output is correct |
2 |
Correct |
11 ms |
19764 KB |
Output is correct |
3 |
Correct |
12 ms |
19804 KB |
Output is correct |
4 |
Correct |
11 ms |
19808 KB |
Output is correct |
5 |
Correct |
12 ms |
19860 KB |
Output is correct |
6 |
Correct |
11 ms |
19780 KB |
Output is correct |
7 |
Correct |
141 ms |
21516 KB |
Output is correct |
8 |
Correct |
11 ms |
19788 KB |
Output is correct |
9 |
Correct |
10 ms |
19788 KB |
Output is correct |
10 |
Correct |
13 ms |
19904 KB |
Output is correct |
11 |
Correct |
10 ms |
19788 KB |
Output is correct |
12 |
Correct |
11 ms |
19916 KB |
Output is correct |
13 |
Correct |
10 ms |
19916 KB |
Output is correct |
14 |
Correct |
11 ms |
19880 KB |
Output is correct |
15 |
Correct |
11 ms |
19788 KB |
Output is correct |
16 |
Correct |
11 ms |
19788 KB |
Output is correct |
17 |
Correct |
11 ms |
19912 KB |
Output is correct |
18 |
Correct |
11 ms |
19916 KB |
Output is correct |
19 |
Correct |
11 ms |
19916 KB |
Output is correct |
20 |
Correct |
11 ms |
19860 KB |
Output is correct |
21 |
Correct |
11 ms |
19884 KB |
Output is correct |
22 |
Correct |
11 ms |
19916 KB |
Output is correct |
23 |
Correct |
11 ms |
19916 KB |
Output is correct |
24 |
Correct |
11 ms |
19968 KB |
Output is correct |
25 |
Correct |
12 ms |
19976 KB |
Output is correct |
26 |
Correct |
12 ms |
19916 KB |
Output is correct |
27 |
Correct |
12 ms |
19916 KB |
Output is correct |
28 |
Correct |
13 ms |
19916 KB |
Output is correct |
29 |
Correct |
12 ms |
19916 KB |
Output is correct |
30 |
Correct |
12 ms |
20004 KB |
Output is correct |
31 |
Correct |
12 ms |
19912 KB |
Output is correct |
32 |
Correct |
13 ms |
19916 KB |
Output is correct |
33 |
Correct |
21 ms |
20516 KB |
Output is correct |
34 |
Correct |
21 ms |
20556 KB |
Output is correct |
35 |
Correct |
43 ms |
20540 KB |
Output is correct |
36 |
Correct |
27 ms |
20584 KB |
Output is correct |
37 |
Correct |
24 ms |
20584 KB |
Output is correct |
38 |
Correct |
54 ms |
20576 KB |
Output is correct |
39 |
Correct |
27 ms |
20636 KB |
Output is correct |
40 |
Correct |
27 ms |
20684 KB |
Output is correct |
41 |
Correct |
65 ms |
20708 KB |
Output is correct |
42 |
Correct |
31 ms |
20820 KB |
Output is correct |
43 |
Correct |
31 ms |
20796 KB |
Output is correct |
44 |
Correct |
89 ms |
20748 KB |
Output is correct |
45 |
Correct |
34 ms |
20932 KB |
Output is correct |
46 |
Correct |
35 ms |
20860 KB |
Output is correct |
47 |
Correct |
87 ms |
20972 KB |
Output is correct |
48 |
Correct |
42 ms |
21032 KB |
Output is correct |
49 |
Correct |
39 ms |
20972 KB |
Output is correct |
50 |
Correct |
106 ms |
21076 KB |
Output is correct |
51 |
Correct |
42 ms |
21032 KB |
Output is correct |
52 |
Correct |
43 ms |
21064 KB |
Output is correct |
53 |
Correct |
129 ms |
21188 KB |
Output is correct |
54 |
Correct |
49 ms |
21204 KB |
Output is correct |
55 |
Correct |
47 ms |
21172 KB |
Output is correct |
56 |
Correct |
147 ms |
21196 KB |
Output is correct |
57 |
Correct |
54 ms |
21332 KB |
Output is correct |
58 |
Correct |
53 ms |
21212 KB |
Output is correct |
59 |
Correct |
167 ms |
21316 KB |
Output is correct |
60 |
Correct |
61 ms |
21412 KB |
Output is correct |
61 |
Correct |
59 ms |
21372 KB |
Output is correct |
62 |
Correct |
188 ms |
21428 KB |
Output is correct |
63 |
Correct |
162 ms |
21508 KB |
Output is correct |
64 |
Correct |
267 ms |
21420 KB |
Output is correct |
65 |
Correct |
208 ms |
21320 KB |
Output is correct |
66 |
Correct |
179 ms |
21444 KB |
Output is correct |
67 |
Correct |
172 ms |
21356 KB |
Output is correct |
68 |
Correct |
97 ms |
21444 KB |
Output is correct |
69 |
Correct |
96 ms |
21444 KB |
Output is correct |
70 |
Correct |
82 ms |
21372 KB |
Output is correct |
71 |
Correct |
88 ms |
21480 KB |
Output is correct |
72 |
Correct |
74 ms |
21444 KB |
Output is correct |
73 |
Correct |
76 ms |
21628 KB |
Output is correct |
74 |
Correct |
110 ms |
21696 KB |
Output is correct |
75 |
Correct |
126 ms |
21636 KB |
Output is correct |
76 |
Correct |
119 ms |
21612 KB |
Output is correct |
77 |
Correct |
114 ms |
21588 KB |
Output is correct |
78 |
Correct |
127 ms |
21572 KB |
Output is correct |
79 |
Correct |
95 ms |
21604 KB |
Output is correct |
80 |
Correct |
111 ms |
21572 KB |
Output is correct |
81 |
Correct |
126 ms |
21680 KB |
Output is correct |
82 |
Correct |
108 ms |
21584 KB |
Output is correct |
83 |
Correct |
134 ms |
21632 KB |
Output is correct |
84 |
Correct |
119 ms |
21632 KB |
Output is correct |
85 |
Correct |
133 ms |
21572 KB |
Output is correct |
86 |
Correct |
130 ms |
21568 KB |
Output is correct |
87 |
Correct |
131 ms |
21636 KB |
Output is correct |
88 |
Correct |
131 ms |
21468 KB |
Output is correct |
89 |
Correct |
138 ms |
21584 KB |
Output is correct |
90 |
Correct |
138 ms |
21580 KB |
Output is correct |
91 |
Correct |
139 ms |
21572 KB |
Output is correct |
92 |
Correct |
146 ms |
21508 KB |
Output is correct |