#include <bits/stdc++.h>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
}
const int MAX = 8e2 + 3;
const ll MOD[] = {1000000007, 998244353};
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
char a[MAX][MAX];
int n, s;
int badTime[MAX][MAX];
bool vs[MAX][MAX];
int d[MAX][MAX];
bool ok(int i, int j) {
return i && j && max(i, j) <= n && a[i][j] != 'T';
}
void Solve() {
cin >> n >> s;
pair <int, int> house;
pair <int, int> start;
queue <pair <int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == 'D') house = {i, j};
if (a[i][j] == 'M') start = {i, j};
if (a[i][j] == 'H') {
vs[i][j] = 1;
q.push({i, j});
}
}
}
while (q.size()) {
auto [u, v] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = u + dx[i];
int y = v + dy[i];
if (ok(x, y) && vs[x][y] == 0 && a[x][y] != 'D') {
vs[x][y] = 1;
badTime[x][y] = badTime[u][v] + 1;
q.push({x, y});
}
}
}
auto check = [&](int val) -> bool {
memset(vs, 0, sizeof vs);
memset(d, 0, sizeof d);
vs[start.first][start.second] = 1;
queue <pair <int, int>> q;
q.push(start);
while (q.size()) {
if (q.front() == house) return 1;
auto [u, v] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = u + dx[i];
int y = v + dy[i];
if (ok(x, y)) {
if (make_pair(x, y) == house) return 1;
if (vs[x][y] == 0 && (badTime[x][y] - (d[u][v] + 1) / s - 1) >= val) {
vs[x][y] = 1;
q.push({x, y});
d[x][y] = d[u][v] + 1;
}
}
}
}
return 0;
};
int ans = -1;
int l = 0, r = n + n;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
l = mid + 1;
ans = mid;
} else r = mid - 1;
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Compilation message
mecho.cpp: In function 'void Solve()':
mecho.cpp:54:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | auto [u, v] = q.front();
| ^
mecho.cpp: In lambda function:
mecho.cpp:77:30: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
77 | auto [u, v] = q.front();
| ^
mecho.cpp: In function 'int32_t main()':
mecho.cpp:115:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:116:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6748 KB |
Output is correct |
5 |
Correct |
1 ms |
6748 KB |
Output is correct |
6 |
Correct |
1 ms |
6768 KB |
Output is correct |
7 |
Correct |
56 ms |
7360 KB |
Output is correct |
8 |
Correct |
1 ms |
6744 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
1 ms |
6748 KB |
Output is correct |
11 |
Correct |
1 ms |
6748 KB |
Output is correct |
12 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
14 |
Correct |
2 ms |
6748 KB |
Output is correct |
15 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
6580 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
6588 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
6780 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
6744 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
6748 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
6776 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
6592 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
6748 KB |
Output isn't correct |
33 |
Incorrect |
3 ms |
6704 KB |
Output isn't correct |
34 |
Incorrect |
3 ms |
6748 KB |
Output isn't correct |
35 |
Correct |
13 ms |
6748 KB |
Output is correct |
36 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
37 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
38 |
Correct |
17 ms |
6936 KB |
Output is correct |
39 |
Incorrect |
5 ms |
6744 KB |
Output isn't correct |
40 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
41 |
Correct |
26 ms |
6744 KB |
Output is correct |
42 |
Incorrect |
5 ms |
7004 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
6780 KB |
Output isn't correct |
44 |
Correct |
28 ms |
7020 KB |
Output is correct |
45 |
Incorrect |
7 ms |
6884 KB |
Output isn't correct |
46 |
Incorrect |
6 ms |
7004 KB |
Output isn't correct |
47 |
Correct |
33 ms |
6888 KB |
Output is correct |
48 |
Incorrect |
7 ms |
7004 KB |
Output isn't correct |
49 |
Incorrect |
7 ms |
6948 KB |
Output isn't correct |
50 |
Correct |
38 ms |
6948 KB |
Output is correct |
51 |
Incorrect |
10 ms |
7260 KB |
Output isn't correct |
52 |
Incorrect |
8 ms |
7004 KB |
Output isn't correct |
53 |
Correct |
46 ms |
7004 KB |
Output is correct |
54 |
Incorrect |
9 ms |
7000 KB |
Output isn't correct |
55 |
Incorrect |
12 ms |
7004 KB |
Output isn't correct |
56 |
Correct |
53 ms |
7004 KB |
Output is correct |
57 |
Incorrect |
10 ms |
7208 KB |
Output isn't correct |
58 |
Incorrect |
10 ms |
7112 KB |
Output isn't correct |
59 |
Correct |
61 ms |
7264 KB |
Output is correct |
60 |
Incorrect |
11 ms |
7256 KB |
Output isn't correct |
61 |
Incorrect |
11 ms |
7260 KB |
Output isn't correct |
62 |
Correct |
72 ms |
7264 KB |
Output is correct |
63 |
Correct |
72 ms |
7264 KB |
Output is correct |
64 |
Correct |
102 ms |
7096 KB |
Output is correct |
65 |
Correct |
100 ms |
7260 KB |
Output is correct |
66 |
Correct |
78 ms |
7448 KB |
Output is correct |
67 |
Correct |
74 ms |
7264 KB |
Output is correct |
68 |
Correct |
33 ms |
7260 KB |
Output is correct |
69 |
Correct |
29 ms |
7260 KB |
Output is correct |
70 |
Correct |
27 ms |
7124 KB |
Output is correct |
71 |
Correct |
26 ms |
7252 KB |
Output is correct |
72 |
Incorrect |
21 ms |
7248 KB |
Output isn't correct |
73 |
Incorrect |
28 ms |
7516 KB |
Output isn't correct |
74 |
Correct |
36 ms |
7508 KB |
Output is correct |
75 |
Correct |
46 ms |
7540 KB |
Output is correct |
76 |
Correct |
40 ms |
7516 KB |
Output is correct |
77 |
Correct |
38 ms |
7516 KB |
Output is correct |
78 |
Correct |
51 ms |
7516 KB |
Output is correct |
79 |
Correct |
42 ms |
7512 KB |
Output is correct |
80 |
Correct |
38 ms |
7372 KB |
Output is correct |
81 |
Correct |
46 ms |
7508 KB |
Output is correct |
82 |
Correct |
44 ms |
7516 KB |
Output is correct |
83 |
Correct |
53 ms |
7316 KB |
Output is correct |
84 |
Correct |
48 ms |
7248 KB |
Output is correct |
85 |
Correct |
61 ms |
7476 KB |
Output is correct |
86 |
Correct |
51 ms |
7256 KB |
Output is correct |
87 |
Correct |
50 ms |
7256 KB |
Output is correct |
88 |
Correct |
52 ms |
7256 KB |
Output is correct |
89 |
Correct |
54 ms |
7260 KB |
Output is correct |
90 |
Correct |
70 ms |
7244 KB |
Output is correct |
91 |
Correct |
55 ms |
7256 KB |
Output is correct |
92 |
Correct |
56 ms |
7248 KB |
Output is correct |