/**
* author : Lăng Trọng Đạt
* created: 2023-08-22
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int int64_t
#define mp make_pair
#define pb push_back
using pii = pair<int, int>;
const int MAXN = 800 + 5;
int dist[MAXN][MAXN];
char g[MAXN][MAXN];
bool vis[MAXN][MAXN];
int x_change[]{1, -1, 0, 0};
int y_change[]{0, 0, 1, -1};
int n, s;
struct Data {
int i, j, cnt, k;
// cnt: number of step, k: : the maximum possible number of minutes that Mecho can continue eating honey at his initial location, while still being able to get home safely.
// @return number of minutes passed
int t() {
return cnt / s + (cnt % s != 0);
}
};
bool vaild(int i, int j, int d) {
return 0 <= i && i < n && 0 <= j && j < n &&
!vis[i][j] && d <= dist[i][j] && g[i][j] != 'T';
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
cin >> n >> s;
queue<pair<pii, int>> q;
queue<Data> q2;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> g[i][j];
if (g[i][j] == 'H') {
q.push({{i, j}, 0});
} else if (g[i][j] == 'M') {
q2.push({i, j, 0, n*n});
}
}
memset(dist, 0x3f3f3f, sizeof(dist));
while (q.size()) {
auto[p, d] = q.front(); q.pop();
auto[i, j] = p;
if (vaild(i, j, d)) {
if (g[i][j] == 'D') continue;
dist[i][j] = d;
for (int k = 0; k < 4; k++) {
q.push({{i + x_change[k], j + y_change[k]}, d + 1});
}
db(i, j, dist[i][j], g[i][j])
}
}
while (q2.size()) {
Data x = q2.front(); q2.pop();
if (vaild(x.i, x.j, x.t()) && x.k >= 0) {
vis[x.i][x.j] = true;
if (x.cnt % s == 0 && x.t() == dist[x.i][x.j]) continue;
x.k = min(x.k, dist[x.i][x.j] - x.t());
// db(x.i, x.j, x.k)
for (int k = 0; k < 4; k++) {
q2.push({x.i + x_change[k], x.j + y_change[k], x.cnt + 1, x.k});
}
if (g[x.i][x.j] == 'D') {
cout << x.k;
return 0;
}
}
}
cout << -1;
}
Compilation message
mecho.cpp: In function 'int32_t main()':
mecho.cpp:58:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | auto[p, d] = q.front(); q.pop();
| ^
mecho.cpp:59:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | auto[i, j] = p;
| ^
mecho.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen("hi.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
5420 KB |
Output isn't correct |
5 |
Correct |
2 ms |
5332 KB |
Output is correct |
6 |
Correct |
2 ms |
5332 KB |
Output is correct |
7 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
2 ms |
5460 KB |
Output is correct |
9 |
Correct |
4 ms |
5460 KB |
Output is correct |
10 |
Correct |
2 ms |
5460 KB |
Output is correct |
11 |
Correct |
2 ms |
5460 KB |
Output is correct |
12 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
13 |
Correct |
21 ms |
10068 KB |
Output is correct |
14 |
Runtime error |
397 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
16 |
Correct |
2 ms |
5332 KB |
Output is correct |
17 |
Incorrect |
2 ms |
5360 KB |
Output isn't correct |
18 |
Correct |
2 ms |
5332 KB |
Output is correct |
19 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
20 |
Correct |
2 ms |
5332 KB |
Output is correct |
21 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
22 |
Correct |
2 ms |
5332 KB |
Output is correct |
23 |
Incorrect |
2 ms |
5332 KB |
Output isn't correct |
24 |
Correct |
2 ms |
5460 KB |
Output is correct |
25 |
Incorrect |
3 ms |
5460 KB |
Output isn't correct |
26 |
Correct |
3 ms |
5460 KB |
Output is correct |
27 |
Incorrect |
2 ms |
5460 KB |
Output isn't correct |
28 |
Correct |
2 ms |
5460 KB |
Output is correct |
29 |
Incorrect |
2 ms |
5460 KB |
Output isn't correct |
30 |
Correct |
2 ms |
5460 KB |
Output is correct |
31 |
Incorrect |
2 ms |
5460 KB |
Output isn't correct |
32 |
Correct |
2 ms |
5460 KB |
Output is correct |
33 |
Incorrect |
6 ms |
5588 KB |
Output isn't correct |
34 |
Correct |
6 ms |
5684 KB |
Output is correct |
35 |
Runtime error |
79 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Incorrect |
8 ms |
5716 KB |
Output isn't correct |
37 |
Correct |
8 ms |
5716 KB |
Output is correct |
38 |
Runtime error |
81 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Incorrect |
9 ms |
5756 KB |
Output isn't correct |
40 |
Correct |
10 ms |
5764 KB |
Output is correct |
41 |
Runtime error |
80 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
11 ms |
5840 KB |
Output isn't correct |
43 |
Correct |
12 ms |
5716 KB |
Output is correct |
44 |
Runtime error |
79 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Incorrect |
13 ms |
5844 KB |
Output isn't correct |
46 |
Correct |
13 ms |
5844 KB |
Output is correct |
47 |
Runtime error |
84 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Incorrect |
15 ms |
5844 KB |
Output isn't correct |
49 |
Correct |
15 ms |
5844 KB |
Output is correct |
50 |
Runtime error |
82 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Incorrect |
17 ms |
5924 KB |
Output isn't correct |
52 |
Correct |
17 ms |
5844 KB |
Output is correct |
53 |
Runtime error |
85 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Incorrect |
20 ms |
5844 KB |
Output isn't correct |
55 |
Correct |
19 ms |
5972 KB |
Output is correct |
56 |
Runtime error |
80 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
21 ms |
5972 KB |
Output isn't correct |
58 |
Correct |
22 ms |
5972 KB |
Output is correct |
59 |
Runtime error |
85 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
24 ms |
6036 KB |
Output isn't correct |
61 |
Correct |
25 ms |
5972 KB |
Output is correct |
62 |
Runtime error |
81 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
58 ms |
6608 KB |
Output is correct |
64 |
Correct |
64 ms |
6700 KB |
Output is correct |
65 |
Correct |
60 ms |
6708 KB |
Output is correct |
66 |
Incorrect |
59 ms |
6612 KB |
Output isn't correct |
67 |
Correct |
51 ms |
6612 KB |
Output is correct |
68 |
Correct |
45 ms |
6904 KB |
Output is correct |
69 |
Correct |
46 ms |
6988 KB |
Output is correct |
70 |
Correct |
45 ms |
6904 KB |
Output is correct |
71 |
Correct |
52 ms |
7056 KB |
Output is correct |
72 |
Incorrect |
38 ms |
6564 KB |
Output isn't correct |
73 |
Runtime error |
109 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
105 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
105 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
108 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
102 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
102 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
100 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
98 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
98 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
100 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
97 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
104 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
102 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
94 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
98 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
93 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |