#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 9000372036854775800;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define print(arr) for (auto i : arr) cout << i << " "; cout << endl;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpi;
typedef vector<int> vi;
typedef pair<int, int> pii;
void setFile(string name) {
freopen((name + "in.txt").c_str(), "r", stdin);
freopen((name + "out.txt").c_str(), "w", stdout);
}
int MOD = 1e9 + 7;
int n, s;
pii pot, home;
bool f(int sleep, vvi dists) {
int n = dists.size();
rep(i, 0, n) {
rep(j, 0, n) {
if (dists[i][j] == INF) continue;
dists[i][j] -= sleep;
if (dists[i][j] <= 0) dists[i][j] = INF;
}
}
// check if valid path exists
vector<vector<bool>> visited(n, vector<bool>(n, false));
queue<vi> q;
q.push({pot.first, pot.second});
visited[pot.first][pot.second] = true;
while (!q.empty()) {
vi curr = q.front(); q.pop();
if (abs(home.first - curr[0]) + abs(home.second - curr[1]) <= s) return true;
vpi dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
trav(d, dir) {
int x = curr[0] + d.first, y = curr[1] + d.second;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (visited[x][y]) continue;
if (dists[x][y] == INF) continue;
visited[x][y] = true;
q.push({x, y});
}
}
return false;
}
int last_true_jump(int lo, int hi, vvi &dists) {
lo--;
for (int dif = hi - lo; dif > 0; dif /= 2) {
while (lo + dif <= hi && f(lo + dif, dists)) { lo += dif; }
}
return lo;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
queue<vi> hives;
vector<vector<char>> grid(n, vector<char>(n));
vvi beedist(n, vi(n, INF));
rep(i, 0, n) {
rep(j, 0, n) {
cin >> grid[i][j];
if (grid[i][j] == 'M') {
pot = {i, j};
} else if (grid[i][j] == 'D') {
home = {i, j};
} else if (grid[i][j] == 'H') {
hives.push({i, j, 0});
beedist[i][j] = 0;
}
}
}
vpi dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
while (!hives.empty()) {
vi curr = hives.front(); hives.pop();
trav(d, dir) {
int x = curr[0] + d.first, y = curr[1] + d.second;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (grid[x][y] == 'T' || grid[x][y] == 'D') continue;
if (beedist[x][y] > curr[2] + 1) {
beedist[x][y] = curr[2] + 1;
hives.push({x, y, curr[2] + 1});
}
}
}
vvi mydist(n, vi(n, INF));
queue<vi> q;
q.push({pot.first, pot.second, 0});
mydist[pot.first][pot.second] = 0;
while (!q.empty()) {
vi curr = q.front(); q.pop();
trav(d, dir) {
int x = curr[0] + d.first, y = curr[1] + d.second;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (grid[x][y] == 'T') continue;
if (mydist[x][y] > curr[2] + 1) {
mydist[x][y] = curr[2] + 1;
q.push({x, y, curr[2] + 1});
}
}
}
vvi dists(n, vi(n, INF));
rep(i, 0, n) {
rep(j, 0, n) {
if (mydist[i][j] == INF) {
dists[i][j] = INF;
continue;
} if (mydist[i][j] == 0) continue;
if (mydist[i][j] % s == 0) {
mydist[i][j] /= s;
} else {
mydist[i][j] /= s;
mydist[i][j]++;
}
dists[i][j] = beedist[i][j] - mydist[i][j];
}
}
// trav(row, dists) {
// trav(i, row) {
// if (i == INF) cout << "-1 ";
// else cout << i << " ";
// }
// cout << endl;
// }
int ans = last_true_jump(0, n*n, dists);
cout << ans << endl;
}
Compilation message
mecho.cpp: In function 'void setFile(std::string)':
mecho.cpp:17:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen((name + "in.txt").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen((name + "out.txt").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
290 ms |
22152 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
600 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
33 |
Correct |
13 ms |
4440 KB |
Output is correct |
34 |
Incorrect |
15 ms |
4444 KB |
Output isn't correct |
35 |
Incorrect |
19 ms |
4444 KB |
Output isn't correct |
36 |
Correct |
18 ms |
5724 KB |
Output is correct |
37 |
Incorrect |
18 ms |
5976 KB |
Output isn't correct |
38 |
Incorrect |
25 ms |
5668 KB |
Output isn't correct |
39 |
Correct |
22 ms |
7316 KB |
Output is correct |
40 |
Incorrect |
22 ms |
7260 KB |
Output isn't correct |
41 |
Incorrect |
32 ms |
7252 KB |
Output isn't correct |
42 |
Correct |
27 ms |
8824 KB |
Output is correct |
43 |
Incorrect |
29 ms |
8772 KB |
Output isn't correct |
44 |
Incorrect |
35 ms |
8848 KB |
Output isn't correct |
45 |
Correct |
33 ms |
10588 KB |
Output is correct |
46 |
Incorrect |
34 ms |
10668 KB |
Output isn't correct |
47 |
Incorrect |
50 ms |
10580 KB |
Output isn't correct |
48 |
Correct |
39 ms |
12376 KB |
Output is correct |
49 |
Incorrect |
41 ms |
12380 KB |
Output isn't correct |
50 |
Incorrect |
78 ms |
12624 KB |
Output isn't correct |
51 |
Correct |
49 ms |
14684 KB |
Output is correct |
52 |
Incorrect |
57 ms |
14676 KB |
Output isn't correct |
53 |
Incorrect |
73 ms |
14728 KB |
Output isn't correct |
54 |
Correct |
53 ms |
16976 KB |
Output is correct |
55 |
Incorrect |
57 ms |
16724 KB |
Output isn't correct |
56 |
Incorrect |
83 ms |
16896 KB |
Output isn't correct |
57 |
Correct |
65 ms |
19336 KB |
Output is correct |
58 |
Incorrect |
64 ms |
19284 KB |
Output isn't correct |
59 |
Incorrect |
81 ms |
19428 KB |
Output isn't correct |
60 |
Correct |
82 ms |
21812 KB |
Output is correct |
61 |
Incorrect |
69 ms |
21840 KB |
Output isn't correct |
62 |
Incorrect |
117 ms |
22096 KB |
Output isn't correct |
63 |
Incorrect |
257 ms |
22040 KB |
Output isn't correct |
64 |
Incorrect |
70 ms |
21840 KB |
Output isn't correct |
65 |
Incorrect |
398 ms |
22192 KB |
Output isn't correct |
66 |
Correct |
371 ms |
22024 KB |
Output is correct |
67 |
Correct |
293 ms |
22076 KB |
Output is correct |
68 |
Incorrect |
173 ms |
22044 KB |
Output isn't correct |
69 |
Incorrect |
77 ms |
21820 KB |
Output isn't correct |
70 |
Incorrect |
172 ms |
22244 KB |
Output isn't correct |
71 |
Incorrect |
199 ms |
22072 KB |
Output isn't correct |
72 |
Correct |
174 ms |
22044 KB |
Output is correct |
73 |
Correct |
127 ms |
22112 KB |
Output is correct |
74 |
Incorrect |
144 ms |
22128 KB |
Output isn't correct |
75 |
Incorrect |
243 ms |
22192 KB |
Output isn't correct |
76 |
Incorrect |
230 ms |
22092 KB |
Output isn't correct |
77 |
Incorrect |
194 ms |
22132 KB |
Output isn't correct |
78 |
Incorrect |
253 ms |
22212 KB |
Output isn't correct |
79 |
Incorrect |
158 ms |
22104 KB |
Output isn't correct |
80 |
Incorrect |
227 ms |
22312 KB |
Output isn't correct |
81 |
Incorrect |
255 ms |
22092 KB |
Output isn't correct |
82 |
Incorrect |
193 ms |
22072 KB |
Output isn't correct |
83 |
Incorrect |
268 ms |
22068 KB |
Output isn't correct |
84 |
Incorrect |
144 ms |
22084 KB |
Output isn't correct |
85 |
Incorrect |
253 ms |
22012 KB |
Output isn't correct |
86 |
Incorrect |
243 ms |
22096 KB |
Output isn't correct |
87 |
Incorrect |
248 ms |
22040 KB |
Output isn't correct |
88 |
Incorrect |
272 ms |
22088 KB |
Output isn't correct |
89 |
Incorrect |
132 ms |
22152 KB |
Output isn't correct |
90 |
Incorrect |
283 ms |
22068 KB |
Output isn't correct |
91 |
Incorrect |
236 ms |
22092 KB |
Output isn't correct |
92 |
Incorrect |
257 ms |
22256 KB |
Output isn't correct |