#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 (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;
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] < w+(dist[x][y]/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:82:14: warning: 'hx' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | if (dist[hx][hy]!=-1) l = w+1;
| ^
mecho.cpp:82:18: warning: 'hy' may be used uninitialized in this function [-Wmaybe-uninitialized]
82 | if (dist[hx][hy]!=-1) l = w+1;
| ^
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
133 ms |
11332 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
18 |
Correct |
1 ms |
408 KB |
Output is correct |
19 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
10 ms |
2520 KB |
Output isn't correct |
34 |
Correct |
10 ms |
2636 KB |
Output is correct |
35 |
Correct |
23 ms |
2524 KB |
Output is correct |
36 |
Incorrect |
12 ms |
3156 KB |
Output isn't correct |
37 |
Correct |
13 ms |
3212 KB |
Output is correct |
38 |
Correct |
29 ms |
3156 KB |
Output is correct |
39 |
Incorrect |
16 ms |
3964 KB |
Output isn't correct |
40 |
Correct |
16 ms |
3948 KB |
Output is correct |
41 |
Correct |
38 ms |
3888 KB |
Output is correct |
42 |
Incorrect |
19 ms |
4764 KB |
Output isn't correct |
43 |
Correct |
19 ms |
4772 KB |
Output is correct |
44 |
Correct |
48 ms |
4668 KB |
Output is correct |
45 |
Incorrect |
31 ms |
5620 KB |
Output isn't correct |
46 |
Correct |
24 ms |
5744 KB |
Output is correct |
47 |
Correct |
61 ms |
5636 KB |
Output is correct |
48 |
Incorrect |
29 ms |
6580 KB |
Output isn't correct |
49 |
Correct |
29 ms |
6588 KB |
Output is correct |
50 |
Correct |
83 ms |
6592 KB |
Output is correct |
51 |
Incorrect |
39 ms |
7712 KB |
Output isn't correct |
52 |
Correct |
34 ms |
7732 KB |
Output is correct |
53 |
Correct |
89 ms |
7620 KB |
Output is correct |
54 |
Incorrect |
40 ms |
8836 KB |
Output isn't correct |
55 |
Correct |
43 ms |
8776 KB |
Output is correct |
56 |
Correct |
104 ms |
8848 KB |
Output is correct |
57 |
Incorrect |
43 ms |
10048 KB |
Output isn't correct |
58 |
Correct |
42 ms |
10044 KB |
Output is correct |
59 |
Correct |
164 ms |
9960 KB |
Output is correct |
60 |
Incorrect |
50 ms |
11324 KB |
Output isn't correct |
61 |
Correct |
51 ms |
11292 KB |
Output is correct |
62 |
Correct |
158 ms |
11348 KB |
Output is correct |
63 |
Correct |
124 ms |
11468 KB |
Output is correct |
64 |
Correct |
177 ms |
11292 KB |
Output is correct |
65 |
Correct |
207 ms |
11256 KB |
Output is correct |
66 |
Incorrect |
132 ms |
11488 KB |
Output isn't correct |
67 |
Correct |
123 ms |
11304 KB |
Output is correct |
68 |
Correct |
73 ms |
11416 KB |
Output is correct |
69 |
Correct |
76 ms |
11400 KB |
Output is correct |
70 |
Correct |
60 ms |
11324 KB |
Output is correct |
71 |
Correct |
74 ms |
11548 KB |
Output is correct |
72 |
Incorrect |
63 ms |
11348 KB |
Output isn't correct |
73 |
Incorrect |
74 ms |
11448 KB |
Output isn't correct |
74 |
Correct |
81 ms |
11400 KB |
Output is correct |
75 |
Correct |
85 ms |
11424 KB |
Output is correct |
76 |
Correct |
88 ms |
11576 KB |
Output is correct |
77 |
Correct |
90 ms |
11416 KB |
Output is correct |
78 |
Correct |
92 ms |
11484 KB |
Output is correct |
79 |
Correct |
78 ms |
11368 KB |
Output is correct |
80 |
Correct |
86 ms |
11336 KB |
Output is correct |
81 |
Correct |
97 ms |
11552 KB |
Output is correct |
82 |
Correct |
79 ms |
11480 KB |
Output is correct |
83 |
Correct |
97 ms |
11344 KB |
Output is correct |
84 |
Correct |
88 ms |
11384 KB |
Output is correct |
85 |
Correct |
88 ms |
11448 KB |
Output is correct |
86 |
Correct |
103 ms |
11424 KB |
Output is correct |
87 |
Correct |
127 ms |
11348 KB |
Output is correct |
88 |
Correct |
103 ms |
11388 KB |
Output is correct |
89 |
Correct |
106 ms |
11348 KB |
Output is correct |
90 |
Correct |
130 ms |
11320 KB |
Output is correct |
91 |
Correct |
109 ms |
11412 KB |
Output is correct |
92 |
Correct |
107 ms |
11372 KB |
Output is correct |