#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) {
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 + (cnt % s > 0 ? 1 : 0) + 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=dist[sx][sy]-1, mid;
while(l<=r) {
mid = (l+r)/2;
if(test(mid)) l = mid + 1;
else r = mid - 1;
}
cout << l - 1 << 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
37 ms |
6388 KB |
Output isn't correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
14 |
Correct |
2 ms |
2652 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
4700 KB |
Output isn't correct |
34 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
35 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
36 |
Incorrect |
5 ms |
4696 KB |
Output isn't correct |
37 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
38 |
Incorrect |
1 ms |
4748 KB |
Output isn't correct |
39 |
Incorrect |
8 ms |
4952 KB |
Output isn't correct |
40 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
41 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
42 |
Incorrect |
8 ms |
4956 KB |
Output isn't correct |
43 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
44 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
45 |
Incorrect |
11 ms |
4952 KB |
Output isn't correct |
46 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
47 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
48 |
Incorrect |
11 ms |
5124 KB |
Output isn't correct |
49 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
50 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
51 |
Incorrect |
13 ms |
5212 KB |
Output isn't correct |
52 |
Incorrect |
2 ms |
5212 KB |
Output isn't correct |
53 |
Incorrect |
2 ms |
5212 KB |
Output isn't correct |
54 |
Incorrect |
15 ms |
5724 KB |
Output isn't correct |
55 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
56 |
Incorrect |
2 ms |
5468 KB |
Output isn't correct |
57 |
Incorrect |
17 ms |
5920 KB |
Output isn't correct |
58 |
Incorrect |
3 ms |
5724 KB |
Output isn't correct |
59 |
Incorrect |
3 ms |
5724 KB |
Output isn't correct |
60 |
Incorrect |
19 ms |
6200 KB |
Output isn't correct |
61 |
Incorrect |
3 ms |
6232 KB |
Output isn't correct |
62 |
Incorrect |
3 ms |
6236 KB |
Output isn't correct |
63 |
Incorrect |
6 ms |
6236 KB |
Output isn't correct |
64 |
Incorrect |
6 ms |
6236 KB |
Output isn't correct |
65 |
Incorrect |
6 ms |
6308 KB |
Output isn't correct |
66 |
Incorrect |
17 ms |
6212 KB |
Output isn't correct |
67 |
Correct |
10 ms |
6236 KB |
Output is correct |
68 |
Incorrect |
21 ms |
6236 KB |
Output isn't correct |
69 |
Incorrect |
20 ms |
6280 KB |
Output isn't correct |
70 |
Incorrect |
20 ms |
6232 KB |
Output isn't correct |
71 |
Incorrect |
20 ms |
6500 KB |
Output isn't correct |
72 |
Incorrect |
30 ms |
6236 KB |
Output isn't correct |
73 |
Correct |
51 ms |
6236 KB |
Output is correct |
74 |
Incorrect |
84 ms |
6236 KB |
Output isn't correct |
75 |
Incorrect |
78 ms |
6468 KB |
Output isn't correct |
76 |
Incorrect |
91 ms |
6468 KB |
Output isn't correct |
77 |
Incorrect |
84 ms |
6236 KB |
Output isn't correct |
78 |
Correct |
23 ms |
6236 KB |
Output is correct |
79 |
Incorrect |
10 ms |
6456 KB |
Output isn't correct |
80 |
Incorrect |
9 ms |
6236 KB |
Output isn't correct |
81 |
Incorrect |
8 ms |
6236 KB |
Output isn't correct |
82 |
Incorrect |
10 ms |
6452 KB |
Output isn't correct |
83 |
Correct |
113 ms |
6236 KB |
Output is correct |
84 |
Correct |
113 ms |
6232 KB |
Output is correct |
85 |
Correct |
115 ms |
6392 KB |
Output is correct |
86 |
Correct |
116 ms |
6392 KB |
Output is correct |
87 |
Correct |
104 ms |
6392 KB |
Output is correct |
88 |
Correct |
138 ms |
6616 KB |
Output is correct |
89 |
Correct |
147 ms |
6236 KB |
Output is correct |
90 |
Correct |
133 ms |
6372 KB |
Output is correct |
91 |
Correct |
138 ms |
6372 KB |
Output is correct |
92 |
Correct |
132 ms |
6236 KB |
Output is correct |