#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define sp << " " <<
#define int long long
#define vi vector<int>
#define pb push_back
#define F(xxx,yyy) for (int xxx=1;xxx<=yyy;xxx++)
#define pii pair<int,int>
#define ordered_set tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update>
void solve() {
int n,k;
cin >> n >> k;
char grid[n+1][n+1];
queue<pii> q;
vector<vi> bee(n+1,vi(n+1,-1));
int sx,sy,hx,hy;
F(i,n) {
string s;
cin >> s;
F(j,n) {
grid[i][j] = s[j-1];
if (s[j-1] == 'H') {
q.push({i,j});
bee[i][j] = 0;
}
if (s[j-1] == 'D') {
hx = i;
hy = j;
}
if (s[j-1] == 'M') {
sx = i;
sy = j;
}
}
}
vi dx = {1,0,-1,0};
vi dy = {0,1,0,-1};
while (!q.empty()) {
pii f = q.front();
q.pop();
int x = f.first;
int y = f.second;
for (int i=0;i<4;i++) {
int gx = x+dx[i];
int gy = y+dy[i];
if (gx < 1 || gx > n) continue;
if (gy < 1 || gy > n) continue;
if (grid[gx][gy] == 'T') continue;
if (grid[gx][gy] == 'D') continue;
if (bee[gx][gy] != -1) continue;
bee[gx][gy] = bee[x][y]+1;
q.push({gx,gy});
}
}
int l = 0;
int r = 1e9;
while (l<=r) {
int w = (l+r) >> 1;
if (w >= bee[sx][sy]) {
r = w-1;
continue;
}
vector<vi> dist(n+1,vi(n+1,-1));
dist[sx][sy] = 0;
q.push({sx,sy});
while (!q.empty()) {
pii f = q.front();
q.pop();
int x = f.first;
int y = f.second;
for (int i=0;i<4;i++) {
int gx = x+dx[i];
int gy = y+dy[i];
if (gx < 1 || gx > n) continue;
if (gy < 1 || gy > n) continue;
if (grid[gx][gy] == 'T') continue;
if (dist[gx][gy] != -1) continue;
if (bee[gx][gy] != -1 && bee[gx][gy] < w+((dist[x][y]+k-1)/k)+1) continue;
dist[gx][gy] = dist[x][y]+1;
q.push({gx,gy});
}
}
if (dist[hx][hy]!=-1) l = w+1;
else r = w-1;
}
cout << r << endl;
}
signed main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while (t --> 0) solve();
}
Compilation message
mecho.cpp: In function 'void solve()':
mecho.cpp:63:22: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
63 | if (w >= bee[sx][sy]) {
| ^
mecho.cpp:63:18: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
63 | if (w >= bee[sx][sy]) {
| ^
mecho.cpp:87:14: warning: 'hx' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | if (dist[hx][hy]!=-1) l = w+1;
| ^
mecho.cpp:87:18: warning: 'hy' may be used uninitialized in this function [-Wmaybe-uninitialized]
87 | if (dist[hx][hy]!=-1) l = w+1;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
7 |
Incorrect |
83 ms |
11316 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
1 ms |
452 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
572 KB |
Output isn't correct |
26 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
2608 KB |
Output isn't correct |
34 |
Incorrect |
6 ms |
2856 KB |
Output isn't correct |
35 |
Incorrect |
18 ms |
2612 KB |
Output isn't correct |
36 |
Incorrect |
7 ms |
3232 KB |
Output isn't correct |
37 |
Incorrect |
6 ms |
3212 KB |
Output isn't correct |
38 |
Incorrect |
24 ms |
3200 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
4012 KB |
Output isn't correct |
40 |
Incorrect |
8 ms |
4012 KB |
Output isn't correct |
41 |
Incorrect |
30 ms |
4052 KB |
Output isn't correct |
42 |
Incorrect |
8 ms |
4828 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
4872 KB |
Output isn't correct |
44 |
Incorrect |
40 ms |
4840 KB |
Output isn't correct |
45 |
Incorrect |
14 ms |
5748 KB |
Output isn't correct |
46 |
Incorrect |
14 ms |
5828 KB |
Output isn't correct |
47 |
Incorrect |
50 ms |
5748 KB |
Output isn't correct |
48 |
Incorrect |
16 ms |
6744 KB |
Output isn't correct |
49 |
Incorrect |
19 ms |
6768 KB |
Output isn't correct |
50 |
Incorrect |
60 ms |
6700 KB |
Output isn't correct |
51 |
Incorrect |
19 ms |
7864 KB |
Output isn't correct |
52 |
Incorrect |
21 ms |
7836 KB |
Output isn't correct |
53 |
Incorrect |
70 ms |
7880 KB |
Output isn't correct |
54 |
Incorrect |
24 ms |
9016 KB |
Output isn't correct |
55 |
Incorrect |
27 ms |
9008 KB |
Output isn't correct |
56 |
Incorrect |
92 ms |
9012 KB |
Output isn't correct |
57 |
Incorrect |
24 ms |
10512 KB |
Output isn't correct |
58 |
Incorrect |
22 ms |
10552 KB |
Output isn't correct |
59 |
Incorrect |
114 ms |
10268 KB |
Output isn't correct |
60 |
Incorrect |
27 ms |
11696 KB |
Output isn't correct |
61 |
Incorrect |
30 ms |
11656 KB |
Output isn't correct |
62 |
Incorrect |
181 ms |
11616 KB |
Output isn't correct |
63 |
Correct |
102 ms |
11588 KB |
Output is correct |
64 |
Incorrect |
163 ms |
11836 KB |
Output isn't correct |
65 |
Correct |
154 ms |
11624 KB |
Output is correct |
66 |
Correct |
109 ms |
11596 KB |
Output is correct |
67 |
Correct |
106 ms |
11832 KB |
Output is correct |
68 |
Incorrect |
46 ms |
11700 KB |
Output isn't correct |
69 |
Incorrect |
49 ms |
11620 KB |
Output isn't correct |
70 |
Incorrect |
44 ms |
11604 KB |
Output isn't correct |
71 |
Incorrect |
35 ms |
11668 KB |
Output isn't correct |
72 |
Correct |
31 ms |
11652 KB |
Output is correct |
73 |
Correct |
40 ms |
11704 KB |
Output is correct |
74 |
Incorrect |
58 ms |
11708 KB |
Output isn't correct |
75 |
Incorrect |
70 ms |
11592 KB |
Output isn't correct |
76 |
Incorrect |
59 ms |
11664 KB |
Output isn't correct |
77 |
Incorrect |
62 ms |
11700 KB |
Output isn't correct |
78 |
Correct |
65 ms |
11668 KB |
Output is correct |
79 |
Incorrect |
56 ms |
11604 KB |
Output isn't correct |
80 |
Incorrect |
61 ms |
11604 KB |
Output isn't correct |
81 |
Incorrect |
70 ms |
11636 KB |
Output isn't correct |
82 |
Incorrect |
62 ms |
12020 KB |
Output isn't correct |
83 |
Incorrect |
77 ms |
11500 KB |
Output isn't correct |
84 |
Incorrect |
68 ms |
11492 KB |
Output isn't correct |
85 |
Incorrect |
66 ms |
11664 KB |
Output isn't correct |
86 |
Incorrect |
76 ms |
11936 KB |
Output isn't correct |
87 |
Incorrect |
71 ms |
11676 KB |
Output isn't correct |
88 |
Incorrect |
69 ms |
11684 KB |
Output isn't correct |
89 |
Incorrect |
75 ms |
11604 KB |
Output isn't correct |
90 |
Incorrect |
78 ms |
11668 KB |
Output isn't correct |
91 |
Incorrect |
80 ms |
11660 KB |
Output isn't correct |
92 |
Incorrect |
81 ms |
11688 KB |
Output isn't correct |