#include <bits/stdc++.h>
using namespace std;
int main()
{
int N, S;
cin >> N >> S;
vector<string> _map(N + 2);
string t = string(N+2, 'T');
_map[0] = t;
_map[N+1] = t;
for(int i = 1; i <= N; i ++){
cin >> _map[i];
_map[i] = "T"+_map[i] + "T";
}
int sx, sy, ex, ey;
vector<pair<int, int>> bees;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j ++){
if(_map[i][j] == 'M'){
sx = j;
sy = i;
_map[i][j]='G';
}
if(_map[i][j] == 'D'){
ex = j;
ey = i;
}
if(_map[i][j] == 'H'){
_map[i][j] == 'T';
bees.push_back({i, j});
}
}
}
vector<int> dx = {1,-1,0,0}, dy = {0,0,1,-1};
int l = 0, r = N * N + 10;
bool gr = false;
while(l + 1 < r){
int mid = (l + r) / 2;
//cout << mid << " " << l << " " << r << endl;
vector<string> arr = _map;
vector<vector<int>> been(N + 2, vector<int>(N + 2));
queue<pair<int, int>> bee, bee2, moves;
moves.push({sy, sx});
for(auto[l, r] : bees){
bee.push({l, r});
}
bool reach = false;
for(int j = 0; j < mid; j ++){
while(!bee.empty()){
int a = bee.front().first, b = bee.front().second;
bee.pop();
for(int i = 0; i < 4; i ++){
if(arr[a+dy[i]][b+dx[i]] == 'G'){
arr[a+dy[i]][b+dx[i]] = 'T';
bee2.push({a+dy[i], b+dx[i]});
}
}
}
swap(bee, bee2);
}
int it = 0;
while(!moves.empty() && !reach){
while(!moves.empty()){
int a = moves.front().first, b = moves.front().second;
//cout << a << " " << b << " " << mid << endl;
if(been[a][b] >= (it + 1) * S)break;
if(arr[a][b]=='D'){reach = true;gr = true;break;}
moves.pop();
//cout << arr[a][b] << endl;
if(arr[a][b] == 'T')continue;
for(int i = 0; i < 4; i ++){
// cout << a + dy[i] << " " << endl;
if(!been[a+dy[i]][b+dx[i]] && arr[a+dy[i]][b+dx[i]] != 'T'){
been[a+dy[i]][b+dx[i]] = been[a][b] + 1;
moves.push({a+dy[i], b+dx[i]});
}
}
}
//cout << " ?" << endl;
while(!bee.empty()){
int a = bee.front().first, b = bee.front().second;
bee.pop();
for(int i = 0; i < 4; i ++){
if(arr[a+dy[i]][b+dx[i]] == 'G'){
arr[a+dy[i]][b+dx[i]] = 'T';
bee2.push({a+dy[i], b+dx[i]});
}
}
}
swap(bee, bee2);
it++;
}
if(reach){
l = mid;
}
else{
r = mid;
}
}
cout << (gr ? l : -1) << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:31:28: warning: value computed is not used [-Wunused-value]
31 | _map[i][j] == 'T';
mecho.cpp:17:17: warning: variable 'ex' set but not used [-Wunused-but-set-variable]
17 | int sx, sy, ex, ey;
| ^~
mecho.cpp:17:21: warning: variable 'ey' set but not used [-Wunused-but-set-variable]
17 | int sx, sy, ex, ey;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
193 ms |
4908 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
20 ms |
1200 KB |
Output is correct |
34 |
Correct |
17 ms |
1108 KB |
Output is correct |
35 |
Correct |
27 ms |
1108 KB |
Output is correct |
36 |
Correct |
30 ms |
1436 KB |
Output is correct |
37 |
Correct |
25 ms |
1364 KB |
Output is correct |
38 |
Correct |
33 ms |
1452 KB |
Output is correct |
39 |
Correct |
33 ms |
1748 KB |
Output is correct |
40 |
Correct |
29 ms |
1748 KB |
Output is correct |
41 |
Correct |
43 ms |
1756 KB |
Output is correct |
42 |
Correct |
40 ms |
2004 KB |
Output is correct |
43 |
Correct |
40 ms |
2084 KB |
Output is correct |
44 |
Correct |
54 ms |
2088 KB |
Output is correct |
45 |
Correct |
50 ms |
2388 KB |
Output is correct |
46 |
Correct |
43 ms |
2460 KB |
Output is correct |
47 |
Correct |
64 ms |
2388 KB |
Output is correct |
48 |
Correct |
61 ms |
2748 KB |
Output is correct |
49 |
Correct |
51 ms |
2824 KB |
Output is correct |
50 |
Correct |
81 ms |
2772 KB |
Output is correct |
51 |
Correct |
85 ms |
3304 KB |
Output is correct |
52 |
Correct |
59 ms |
3284 KB |
Output is correct |
53 |
Correct |
93 ms |
3288 KB |
Output is correct |
54 |
Correct |
87 ms |
3752 KB |
Output is correct |
55 |
Correct |
73 ms |
3748 KB |
Output is correct |
56 |
Correct |
109 ms |
3756 KB |
Output is correct |
57 |
Correct |
106 ms |
4288 KB |
Output is correct |
58 |
Correct |
96 ms |
4280 KB |
Output is correct |
59 |
Correct |
128 ms |
4276 KB |
Output is correct |
60 |
Correct |
123 ms |
4884 KB |
Output is correct |
61 |
Correct |
113 ms |
4804 KB |
Output is correct |
62 |
Correct |
157 ms |
4792 KB |
Output is correct |
63 |
Incorrect |
185 ms |
4740 KB |
Output isn't correct |
64 |
Correct |
231 ms |
4800 KB |
Output is correct |
65 |
Correct |
205 ms |
4864 KB |
Output is correct |
66 |
Correct |
209 ms |
4804 KB |
Output is correct |
67 |
Correct |
237 ms |
4800 KB |
Output is correct |
68 |
Incorrect |
188 ms |
4820 KB |
Output isn't correct |
69 |
Correct |
155 ms |
4820 KB |
Output is correct |
70 |
Correct |
182 ms |
4820 KB |
Output is correct |
71 |
Correct |
209 ms |
4824 KB |
Output is correct |
72 |
Correct |
198 ms |
4824 KB |
Output is correct |
73 |
Correct |
169 ms |
5104 KB |
Output is correct |
74 |
Correct |
209 ms |
5092 KB |
Output is correct |
75 |
Correct |
232 ms |
5112 KB |
Output is correct |
76 |
Correct |
223 ms |
5168 KB |
Output is correct |
77 |
Correct |
223 ms |
5104 KB |
Output is correct |
78 |
Correct |
212 ms |
5012 KB |
Output is correct |
79 |
Correct |
188 ms |
4984 KB |
Output is correct |
80 |
Correct |
197 ms |
5076 KB |
Output is correct |
81 |
Correct |
206 ms |
5092 KB |
Output is correct |
82 |
Correct |
233 ms |
5072 KB |
Output is correct |
83 |
Incorrect |
196 ms |
5024 KB |
Output isn't correct |
84 |
Correct |
213 ms |
5032 KB |
Output is correct |
85 |
Correct |
220 ms |
5036 KB |
Output is correct |
86 |
Correct |
206 ms |
5028 KB |
Output is correct |
87 |
Correct |
222 ms |
5036 KB |
Output is correct |
88 |
Incorrect |
195 ms |
4980 KB |
Output isn't correct |
89 |
Correct |
237 ms |
4980 KB |
Output is correct |
90 |
Correct |
213 ms |
4968 KB |
Output is correct |
91 |
Correct |
193 ms |
4964 KB |
Output is correct |
92 |
Correct |
193 ms |
4968 KB |
Output is correct |