#include <bits/stdc++.h>
using namespace std;
#define ll long long
int N, S;
vector<string> V;
vector<vector<int>> P;
int dirs[4][2] = {{1, 0},{0, 1},{-1, 0},{0, -1}};
struct info{
int r, c, d;
info(){}
info(int _r, int _c, int _d): r(_r), c(_c), d(_d) {}
};
void precompute(vector<pair<int, int>> starts){
queue<info> q;
for(int i = 0; i < (int)starts.size(); i++){
q.push(info(starts[i].first, starts[i].second, 0));
P[starts[i].first][starts[i].second] = 0;
}
while(!q.empty()){
info curr = q.front();
q.pop();
int r = curr.r;
int c = curr.c;
int depth = curr.d;
for(int i = 0; i < 4; i++){
int nr = r + dirs[i][0];
int nc = c + dirs[i][1];
if(nr < 0 || nr >= N || nc < 0 || nc >= N || (V[nr][nc] != 'G' && V[nr][nc] != 'M') || P[nr][nc] != INT_MAX) continue;
P[nr][nc] = depth+1;
q.push(info(nr, nc, depth+1));
}
}
}
bool solver(int r, int c, int dr, int dc, int l){
queue<info> q;
q.push(info(r, c, l+1));
vector<vector<bool>> visited(N, vector<bool>(N, false));
while(!q.empty()){
info curr = q.front();
q.pop();
int r = curr.r;
int c = curr.c;
int depth = curr.d;
// cout<<"DEBUG-->"<<l<<" "<<r<<" "<<c<<endl;
visited[r][c] = true;
for(int i = 0; i < 4; i++){
int nr = r + dirs[i][0];
int nc = c + dirs[i][1];
if(nr < 0 || nr >= N || nc < 0 || nc >= N || (V[nr][nc] != 'G' && V[nr][nc] != 'D') || visited[nr][nc] || P[nr][nc] <= (l + (int)((depth-l)/S))) continue;
visited[nr][nc] = true;
q.push(info(nr, nc, depth+1));
}
}
return visited[dr][dc];
}
void solve(){
cin>>N>>S;
V.assign(N, "");
P.assign(N, vector<int>(N, INT_MAX));
vector<pair<int, int>> starts;
int sr, sc, dr, dc;
for(int i = 0; i < N; i++){
cin>>V[i];
for(int j = 0; j < N; j++)
if(V[i][j] == 'H') starts.push_back({i, j});
else if(V[i][j] == 'M') sr = i, sc = j;
else if(V[i][j] == 'D') dr = i, dc = j;
}
precompute(starts);
int low = 0;
int high = P[sr][sc];
int ans = -1;
while(low < high){
int mid = low + (high - low)/2;
if(solver(sr, sc, dr, dc, mid)){
ans = mid;
low = mid+1;
}else high = mid;
}
cout<<ans<<endl;
}
int main(){
solve();
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:117:12: warning: 'dr' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | if(solver(sr, sc, dr, dc, mid)){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
mecho.cpp:110:21: warning: 'sc' may be used uninitialized in this function [-Wmaybe-uninitialized]
110 | int high = P[sr][sc];
| ^
mecho.cpp:110:17: warning: 'sr' may be used uninitialized in this function [-Wmaybe-uninitialized]
110 | int high = P[sr][sc];
| ^
mecho.cpp:117:12: warning: 'dc' may be used uninitialized in this function [-Wmaybe-uninitialized]
117 | if(solver(sr, sc, dr, dc, mid)){
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
93 ms |
4160 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
300 KB |
Output is correct |
14 |
Correct |
2 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
304 KB |
Output is correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 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 |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 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 |
7 ms |
1080 KB |
Output is correct |
34 |
Correct |
7 ms |
1076 KB |
Output is correct |
35 |
Correct |
21 ms |
1092 KB |
Output is correct |
36 |
Correct |
9 ms |
1332 KB |
Output is correct |
37 |
Correct |
8 ms |
1236 KB |
Output is correct |
38 |
Correct |
30 ms |
1316 KB |
Output is correct |
39 |
Correct |
10 ms |
1564 KB |
Output is correct |
40 |
Correct |
12 ms |
1464 KB |
Output is correct |
41 |
Correct |
43 ms |
1500 KB |
Output is correct |
42 |
Correct |
13 ms |
1968 KB |
Output is correct |
43 |
Correct |
18 ms |
1988 KB |
Output is correct |
44 |
Correct |
48 ms |
1876 KB |
Output is correct |
45 |
Correct |
15 ms |
2352 KB |
Output is correct |
46 |
Correct |
15 ms |
2356 KB |
Output is correct |
47 |
Correct |
59 ms |
2228 KB |
Output is correct |
48 |
Correct |
16 ms |
2644 KB |
Output is correct |
49 |
Correct |
20 ms |
2688 KB |
Output is correct |
50 |
Correct |
81 ms |
2624 KB |
Output is correct |
51 |
Correct |
20 ms |
3080 KB |
Output is correct |
52 |
Correct |
21 ms |
3000 KB |
Output is correct |
53 |
Correct |
80 ms |
2952 KB |
Output is correct |
54 |
Correct |
23 ms |
3312 KB |
Output is correct |
55 |
Correct |
25 ms |
3284 KB |
Output is correct |
56 |
Correct |
93 ms |
3280 KB |
Output is correct |
57 |
Correct |
28 ms |
3684 KB |
Output is correct |
58 |
Correct |
30 ms |
3660 KB |
Output is correct |
59 |
Correct |
111 ms |
3756 KB |
Output is correct |
60 |
Correct |
31 ms |
3984 KB |
Output is correct |
61 |
Correct |
31 ms |
3984 KB |
Output is correct |
62 |
Correct |
128 ms |
4124 KB |
Output is correct |
63 |
Correct |
118 ms |
4112 KB |
Output is correct |
64 |
Correct |
205 ms |
4120 KB |
Output is correct |
65 |
Correct |
169 ms |
4012 KB |
Output is correct |
66 |
Correct |
144 ms |
4124 KB |
Output is correct |
67 |
Correct |
156 ms |
4120 KB |
Output is correct |
68 |
Correct |
63 ms |
4104 KB |
Output is correct |
69 |
Correct |
47 ms |
4172 KB |
Output is correct |
70 |
Correct |
41 ms |
4104 KB |
Output is correct |
71 |
Correct |
46 ms |
4080 KB |
Output is correct |
72 |
Correct |
39 ms |
4052 KB |
Output is correct |
73 |
Correct |
49 ms |
4544 KB |
Output is correct |
74 |
Correct |
70 ms |
4348 KB |
Output is correct |
75 |
Correct |
68 ms |
4452 KB |
Output is correct |
76 |
Correct |
69 ms |
4428 KB |
Output is correct |
77 |
Correct |
71 ms |
4428 KB |
Output is correct |
78 |
Correct |
109 ms |
4316 KB |
Output is correct |
79 |
Correct |
63 ms |
4388 KB |
Output is correct |
80 |
Correct |
64 ms |
4400 KB |
Output is correct |
81 |
Correct |
79 ms |
4412 KB |
Output is correct |
82 |
Correct |
69 ms |
4404 KB |
Output is correct |
83 |
Correct |
94 ms |
4344 KB |
Output is correct |
84 |
Correct |
78 ms |
4308 KB |
Output is correct |
85 |
Correct |
85 ms |
4260 KB |
Output is correct |
86 |
Correct |
95 ms |
4328 KB |
Output is correct |
87 |
Correct |
84 ms |
4320 KB |
Output is correct |
88 |
Correct |
89 ms |
4300 KB |
Output is correct |
89 |
Correct |
86 ms |
4168 KB |
Output is correct |
90 |
Correct |
97 ms |
4136 KB |
Output is correct |
91 |
Correct |
111 ms |
4172 KB |
Output is correct |
92 |
Correct |
96 ms |
4256 KB |
Output is correct |