#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 = -1, 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 << l << 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;
| ^~
mecho.cpp:38:10: warning: variable 'gr' set but not used [-Wunused-but-set-variable]
38 | bool gr = false;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
220 ms |
4908 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 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 |
0 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 |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
19 ms |
1192 KB |
Output is correct |
34 |
Correct |
17 ms |
1196 KB |
Output is correct |
35 |
Correct |
24 ms |
1200 KB |
Output is correct |
36 |
Correct |
26 ms |
1364 KB |
Output is correct |
37 |
Correct |
23 ms |
1452 KB |
Output is correct |
38 |
Correct |
32 ms |
1460 KB |
Output is correct |
39 |
Correct |
39 ms |
1620 KB |
Output is correct |
40 |
Correct |
27 ms |
1748 KB |
Output is correct |
41 |
Correct |
42 ms |
1748 KB |
Output is correct |
42 |
Correct |
41 ms |
2072 KB |
Output is correct |
43 |
Correct |
35 ms |
2004 KB |
Output is correct |
44 |
Correct |
53 ms |
2080 KB |
Output is correct |
45 |
Correct |
48 ms |
2448 KB |
Output is correct |
46 |
Correct |
45 ms |
2388 KB |
Output is correct |
47 |
Correct |
63 ms |
2464 KB |
Output is correct |
48 |
Correct |
60 ms |
2836 KB |
Output is correct |
49 |
Correct |
52 ms |
2828 KB |
Output is correct |
50 |
Correct |
86 ms |
2864 KB |
Output is correct |
51 |
Correct |
73 ms |
3284 KB |
Output is correct |
52 |
Correct |
62 ms |
3284 KB |
Output is correct |
53 |
Correct |
99 ms |
3300 KB |
Output is correct |
54 |
Correct |
83 ms |
3768 KB |
Output is correct |
55 |
Correct |
74 ms |
3668 KB |
Output is correct |
56 |
Correct |
109 ms |
3668 KB |
Output is correct |
57 |
Correct |
104 ms |
4288 KB |
Output is correct |
58 |
Correct |
113 ms |
4276 KB |
Output is correct |
59 |
Correct |
124 ms |
4268 KB |
Output is correct |
60 |
Correct |
123 ms |
4828 KB |
Output is correct |
61 |
Correct |
106 ms |
4800 KB |
Output is correct |
62 |
Correct |
141 ms |
4812 KB |
Output is correct |
63 |
Correct |
198 ms |
4804 KB |
Output is correct |
64 |
Correct |
234 ms |
4796 KB |
Output is correct |
65 |
Correct |
225 ms |
4796 KB |
Output is correct |
66 |
Correct |
208 ms |
4820 KB |
Output is correct |
67 |
Correct |
200 ms |
4812 KB |
Output is correct |
68 |
Correct |
197 ms |
4820 KB |
Output is correct |
69 |
Correct |
166 ms |
4824 KB |
Output is correct |
70 |
Correct |
170 ms |
4824 KB |
Output is correct |
71 |
Correct |
197 ms |
4820 KB |
Output is correct |
72 |
Correct |
189 ms |
4820 KB |
Output is correct |
73 |
Correct |
176 ms |
5216 KB |
Output is correct |
74 |
Correct |
209 ms |
5092 KB |
Output is correct |
75 |
Correct |
224 ms |
5116 KB |
Output is correct |
76 |
Correct |
213 ms |
5100 KB |
Output is correct |
77 |
Correct |
229 ms |
5104 KB |
Output is correct |
78 |
Correct |
200 ms |
5076 KB |
Output is correct |
79 |
Correct |
204 ms |
5084 KB |
Output is correct |
80 |
Correct |
204 ms |
5068 KB |
Output is correct |
81 |
Correct |
195 ms |
5084 KB |
Output is correct |
82 |
Correct |
197 ms |
5076 KB |
Output is correct |
83 |
Correct |
201 ms |
5164 KB |
Output is correct |
84 |
Correct |
217 ms |
5004 KB |
Output is correct |
85 |
Correct |
218 ms |
5028 KB |
Output is correct |
86 |
Correct |
206 ms |
5048 KB |
Output is correct |
87 |
Correct |
195 ms |
5032 KB |
Output is correct |
88 |
Correct |
193 ms |
4948 KB |
Output is correct |
89 |
Correct |
194 ms |
4984 KB |
Output is correct |
90 |
Correct |
196 ms |
4968 KB |
Output is correct |
91 |
Correct |
197 ms |
4976 KB |
Output is correct |
92 |
Correct |
239 ms |
4968 KB |
Output is correct |