#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 4e5 + 5;
int n, s, dist[805][805], sx, sy, ex, ey;
string grid[805];
bool test(int m) {
if(dist[sx][sy] <= m) return false;
queue<pair<int, pair<int,int>>>q;
q.push({0, {sx, sy}});
vector<vector<bool>>visited(n, vector<bool>(n, 0));
while(!q.empty()) {
int cnt = q.front().first, x = q.front().second.first, y = q.front().second.second;
q.pop();
if(visited[x][y]) continue;
visited[x][y] = 1;
++cnt;
int cost = cnt / s + m;
if(x && dist[x-1][y] > cost && grid[x-1][y] == 'G') q.push({cnt, {x-1, y}});
if(y && dist[x][y-1] > cost && grid[x][y-1] == 'G') q.push({cnt, {x, y-1}});
if(x+1<n && dist[x+1][y] > cost && grid[x+1][y] == 'G') q.push({cnt, {x+1, y}});
if(y+1<n && dist[x][y+1] > cost && grid[x][y+1] == 'G') q.push({cnt, {x, y+1}});
}
if(ex && visited[ex-1][ey]) return true;
if(ey && visited[ex][ey-1]) return true;
if(ex+1<n && visited[ex+1][ey]) return true;
if(ey+1<n && visited[ex][ey+1]) return true;
return false;
}
void solve() {
cin >> n >> s;
queue<pair<int,int>>q;
for(int i=0; i<n; ++i) {
cin >> grid[i];
for(int j=0; j<n; ++j) {
dist[i][j] = -1;
if(grid[i][j] == 'H') {
q.push({i, j});
dist[i][j] = 0;
} else if(grid[i][j] == 'M') sx=i, sy=j;
else if(grid[i][j] == 'D') ex=i, ey=j;
}
}
while(!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if(i && dist[i-1][j] == -1 && grid[i-1][j] != 'T' && grid[i-1][j] != 'D') {
dist[i-1][j] = dist[i][j] + 1;
q.push({i-1, j});
}
if(j && dist[i][j-1] == -1 && grid[i][j-1] != 'T' && grid[i][j-1] != 'D') {
dist[i][j-1] = dist[i][j] + 1;
q.push({i, j-1});
}
if(i<n-1 && dist[i+1][j] == -1 && grid[i+1][j] != 'T' && grid[i+1][j] != 'D') {
dist[i+1][j] = dist[i][j] + 1;
q.push({i+1, j});
}
if(j<n-1 && dist[i][j+1] == -1 && grid[i][j+1] != 'T' && grid[i][j+1] != 'D') {
dist[i][j+1] = dist[i][j] + 1;
q.push({i, j+1});
}
}
if(!test(0)) {
cout << -1 << endl;
return;
}
int l=0, r=n*n, mid;
while(l<=r) {
mid = (l+r)/2;
if(test(mid)) l = mid + 1;
else r = mid - 1;
}
cout << r << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
120 ms |
6236 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
420 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2652 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
4 ms |
4700 KB |
Output is correct |
34 |
Correct |
5 ms |
4700 KB |
Output is correct |
35 |
Correct |
29 ms |
4700 KB |
Output is correct |
36 |
Correct |
5 ms |
4700 KB |
Output is correct |
37 |
Correct |
6 ms |
4700 KB |
Output is correct |
38 |
Correct |
37 ms |
5208 KB |
Output is correct |
39 |
Correct |
6 ms |
4956 KB |
Output is correct |
40 |
Correct |
7 ms |
4996 KB |
Output is correct |
41 |
Correct |
61 ms |
4952 KB |
Output is correct |
42 |
Correct |
7 ms |
4992 KB |
Output is correct |
43 |
Correct |
9 ms |
4996 KB |
Output is correct |
44 |
Correct |
64 ms |
5008 KB |
Output is correct |
45 |
Correct |
8 ms |
4956 KB |
Output is correct |
46 |
Correct |
10 ms |
4940 KB |
Output is correct |
47 |
Correct |
71 ms |
4952 KB |
Output is correct |
48 |
Correct |
11 ms |
5468 KB |
Output is correct |
49 |
Correct |
11 ms |
4956 KB |
Output is correct |
50 |
Correct |
85 ms |
5216 KB |
Output is correct |
51 |
Correct |
10 ms |
5208 KB |
Output is correct |
52 |
Correct |
13 ms |
5212 KB |
Output is correct |
53 |
Correct |
109 ms |
5280 KB |
Output is correct |
54 |
Correct |
13 ms |
5508 KB |
Output is correct |
55 |
Correct |
18 ms |
5720 KB |
Output is correct |
56 |
Correct |
126 ms |
5472 KB |
Output is correct |
57 |
Correct |
14 ms |
5720 KB |
Output is correct |
58 |
Correct |
18 ms |
5724 KB |
Output is correct |
59 |
Correct |
145 ms |
5720 KB |
Output is correct |
60 |
Correct |
17 ms |
6236 KB |
Output is correct |
61 |
Correct |
21 ms |
6232 KB |
Output is correct |
62 |
Correct |
170 ms |
6340 KB |
Output is correct |
63 |
Correct |
95 ms |
6312 KB |
Output is correct |
64 |
Correct |
176 ms |
6312 KB |
Output is correct |
65 |
Correct |
143 ms |
6244 KB |
Output is correct |
66 |
Correct |
109 ms |
6236 KB |
Output is correct |
67 |
Correct |
22 ms |
6236 KB |
Output is correct |
68 |
Correct |
42 ms |
6236 KB |
Output is correct |
69 |
Correct |
35 ms |
6236 KB |
Output is correct |
70 |
Correct |
24 ms |
6236 KB |
Output is correct |
71 |
Correct |
32 ms |
6480 KB |
Output is correct |
72 |
Correct |
22 ms |
6236 KB |
Output is correct |
73 |
Correct |
32 ms |
6748 KB |
Output is correct |
74 |
Correct |
87 ms |
6748 KB |
Output is correct |
75 |
Correct |
91 ms |
6744 KB |
Output is correct |
76 |
Correct |
81 ms |
6752 KB |
Output is correct |
77 |
Correct |
79 ms |
6748 KB |
Output is correct |
78 |
Correct |
22 ms |
6488 KB |
Output is correct |
79 |
Correct |
73 ms |
6492 KB |
Output is correct |
80 |
Correct |
75 ms |
6488 KB |
Output is correct |
81 |
Correct |
87 ms |
6492 KB |
Output is correct |
82 |
Correct |
77 ms |
6488 KB |
Output is correct |
83 |
Correct |
104 ms |
6604 KB |
Output is correct |
84 |
Correct |
103 ms |
6488 KB |
Output is correct |
85 |
Correct |
102 ms |
6852 KB |
Output is correct |
86 |
Correct |
101 ms |
6592 KB |
Output is correct |
87 |
Correct |
104 ms |
6488 KB |
Output is correct |
88 |
Correct |
110 ms |
6488 KB |
Output is correct |
89 |
Correct |
124 ms |
6504 KB |
Output is correct |
90 |
Correct |
125 ms |
6492 KB |
Output is correct |
91 |
Correct |
120 ms |
6492 KB |
Output is correct |
92 |
Correct |
122 ms |
6488 KB |
Output is correct |