#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define piipi pair<pii, int>
int N = 1e6;
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, s;
cin >> n >> s;
vector<string> grid(n+2);
string ns = "";
for (int i = 0; i < n+2; i++) ns += "T";
grid[0] = ns;
grid[n+1] = ns;
for (int i = 1; i <= n; i++)
{
cin >> grid[i];
grid[i] = "T" + grid[i] + "T";
}
vector<vector<int>> graph(N);
queue<pii> q;
int home = -1;
int mecho = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int ind = i*(n+2) +j;
if (grid[i][j]=='H') q.push({0, ind});
if (grid[i][j]=='D') home = ind;
if (grid[i][j]=='M')
{
mecho = ind;
graph[ind-1].push_back(ind);
graph[ind+1].push_back(ind);
graph[ind+n+2].push_back(ind);
graph[ind-n-2].push_back(ind);
}
if (grid[i][j-1]=='G') graph[ind].push_back(ind-1);
if (grid[i][j+1]=='G') graph[ind].push_back(ind+1);
if (grid[i+1][j]=='G') graph[ind].push_back(ind+n+2);
if (grid[i-1][j]=='G') graph[ind].push_back(ind-n-2);
}
}
vector<int> dist(N, -1);
while (q.size())
{
int i, d;
tie(d, i) = q.front();
q.pop();
if (dist[i]!=-1) continue;
dist[i] = d;
for (int ele:graph[i])
{
q.push({d+1, ele});
}
}
priority_queue<pii, vector<pii>, less<pii>> toadd;
vector<int> reach(N, 0);
toadd.push({N, home});
for (int i = N-1; i >= 0; i--)
{
while(toadd.size() && toadd.top().first >= i)
{
q.push({0, toadd.top().second});
toadd.pop();
}
while (q.size())
{
int j, d;
tie(d, j) = q.front();
q.pop();
if (reach[j]==1) continue;
reach[j] = 1;
for (int ele:graph[j])
{
if (dist[ele]>=i && d+1 < s) q.push({d+1, ele});
else toadd.push({dist[ele], ele});
}
}
if (reach[mecho])
{
cout << i-1;
return 0;
}
}
cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
31576 KB |
Output is correct |
2 |
Correct |
13 ms |
31580 KB |
Output is correct |
3 |
Correct |
14 ms |
31576 KB |
Output is correct |
4 |
Correct |
14 ms |
31580 KB |
Output is correct |
5 |
Correct |
13 ms |
31580 KB |
Output is correct |
6 |
Correct |
14 ms |
31576 KB |
Output is correct |
7 |
Correct |
228 ms |
53456 KB |
Output is correct |
8 |
Correct |
13 ms |
31580 KB |
Output is correct |
9 |
Correct |
14 ms |
31580 KB |
Output is correct |
10 |
Correct |
13 ms |
31768 KB |
Output is correct |
11 |
Correct |
13 ms |
31580 KB |
Output is correct |
12 |
Correct |
13 ms |
31888 KB |
Output is correct |
13 |
Correct |
14 ms |
31832 KB |
Output is correct |
14 |
Correct |
15 ms |
31832 KB |
Output is correct |
15 |
Correct |
13 ms |
31580 KB |
Output is correct |
16 |
Correct |
13 ms |
31580 KB |
Output is correct |
17 |
Correct |
12 ms |
31580 KB |
Output is correct |
18 |
Correct |
13 ms |
31784 KB |
Output is correct |
19 |
Correct |
13 ms |
31580 KB |
Output is correct |
20 |
Correct |
13 ms |
31580 KB |
Output is correct |
21 |
Correct |
14 ms |
31580 KB |
Output is correct |
22 |
Correct |
12 ms |
31580 KB |
Output is correct |
23 |
Correct |
13 ms |
31836 KB |
Output is correct |
24 |
Correct |
16 ms |
31844 KB |
Output is correct |
25 |
Correct |
13 ms |
31836 KB |
Output is correct |
26 |
Correct |
13 ms |
31836 KB |
Output is correct |
27 |
Correct |
14 ms |
31904 KB |
Output is correct |
28 |
Correct |
13 ms |
31836 KB |
Output is correct |
29 |
Correct |
14 ms |
31836 KB |
Output is correct |
30 |
Correct |
14 ms |
31832 KB |
Output is correct |
31 |
Correct |
15 ms |
31832 KB |
Output is correct |
32 |
Correct |
15 ms |
31836 KB |
Output is correct |
33 |
Correct |
30 ms |
35676 KB |
Output is correct |
34 |
Correct |
25 ms |
35780 KB |
Output is correct |
35 |
Correct |
51 ms |
35672 KB |
Output is correct |
36 |
Correct |
30 ms |
37024 KB |
Output is correct |
37 |
Correct |
33 ms |
36956 KB |
Output is correct |
38 |
Correct |
42 ms |
36944 KB |
Output is correct |
39 |
Correct |
37 ms |
38488 KB |
Output is correct |
40 |
Correct |
38 ms |
38312 KB |
Output is correct |
41 |
Correct |
53 ms |
38428 KB |
Output is correct |
42 |
Correct |
39 ms |
40020 KB |
Output is correct |
43 |
Correct |
38 ms |
40024 KB |
Output is correct |
44 |
Correct |
81 ms |
40016 KB |
Output is correct |
45 |
Correct |
43 ms |
41812 KB |
Output is correct |
46 |
Correct |
48 ms |
41976 KB |
Output is correct |
47 |
Correct |
89 ms |
41856 KB |
Output is correct |
48 |
Correct |
51 ms |
43668 KB |
Output is correct |
49 |
Correct |
50 ms |
43600 KB |
Output is correct |
50 |
Correct |
116 ms |
43688 KB |
Output is correct |
51 |
Correct |
60 ms |
45840 KB |
Output is correct |
52 |
Correct |
55 ms |
45824 KB |
Output is correct |
53 |
Correct |
128 ms |
45652 KB |
Output is correct |
54 |
Correct |
62 ms |
48044 KB |
Output is correct |
55 |
Correct |
62 ms |
47952 KB |
Output is correct |
56 |
Correct |
166 ms |
48064 KB |
Output is correct |
57 |
Correct |
70 ms |
50260 KB |
Output is correct |
58 |
Correct |
70 ms |
50260 KB |
Output is correct |
59 |
Correct |
207 ms |
50696 KB |
Output is correct |
60 |
Correct |
79 ms |
53076 KB |
Output is correct |
61 |
Correct |
79 ms |
53004 KB |
Output is correct |
62 |
Correct |
231 ms |
53104 KB |
Output is correct |
63 |
Correct |
195 ms |
52972 KB |
Output is correct |
64 |
Correct |
152 ms |
52700 KB |
Output is correct |
65 |
Correct |
160 ms |
52784 KB |
Output is correct |
66 |
Correct |
187 ms |
53012 KB |
Output is correct |
67 |
Correct |
202 ms |
52788 KB |
Output is correct |
68 |
Correct |
140 ms |
52836 KB |
Output is correct |
69 |
Correct |
139 ms |
52684 KB |
Output is correct |
70 |
Correct |
135 ms |
52820 KB |
Output is correct |
71 |
Correct |
136 ms |
52844 KB |
Output is correct |
72 |
Correct |
127 ms |
52820 KB |
Output is correct |
73 |
Correct |
261 ms |
54284 KB |
Output is correct |
74 |
Correct |
215 ms |
54108 KB |
Output is correct |
75 |
Correct |
238 ms |
54156 KB |
Output is correct |
76 |
Correct |
245 ms |
54032 KB |
Output is correct |
77 |
Correct |
217 ms |
54096 KB |
Output is correct |
78 |
Correct |
269 ms |
53976 KB |
Output is correct |
79 |
Correct |
192 ms |
53840 KB |
Output is correct |
80 |
Correct |
208 ms |
53968 KB |
Output is correct |
81 |
Correct |
226 ms |
54060 KB |
Output is correct |
82 |
Correct |
208 ms |
53880 KB |
Output is correct |
83 |
Correct |
241 ms |
53840 KB |
Output is correct |
84 |
Correct |
206 ms |
53836 KB |
Output is correct |
85 |
Correct |
199 ms |
53904 KB |
Output is correct |
86 |
Correct |
206 ms |
53792 KB |
Output is correct |
87 |
Correct |
219 ms |
53768 KB |
Output is correct |
88 |
Correct |
255 ms |
53716 KB |
Output is correct |
89 |
Correct |
199 ms |
53588 KB |
Output is correct |
90 |
Correct |
236 ms |
53628 KB |
Output is correct |
91 |
Correct |
198 ms |
53584 KB |
Output is correct |
92 |
Correct |
233 ms |
53652 KB |
Output is correct |