#include <bits/stdc++.h>
using namespace std;
#pragma region y_combinator
#ifndef Y_COMBINATOR_HPP
#define Y_COMBINATOR_HPP
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
#endif
#pragma endregion y_combinator
const int INF = 1e9;
const array<int, 4> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
int main() {
int N, M;
cin >> N >> M;
vector<vector<char>> A(N + 2, vector<char>(M + 2));
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
cin >> A[i][j];
int time = 0;
vector<vector<bool>> vis(N + 2, vector<bool>(M + 2, false)), inq(N + 2, vector<bool>(M + 2, false));
auto dfs = y_combinator([&](auto dfs, int x, int y, int t) -> void {
if (time >= INF || inq[x][y])
return void(time = INF);
if (x <= 0 || x > N || y <= 0 || y > M || vis[x][y])
return;
vis[x][y] = inq[x][y] = true;
time++;
dfs(x + dx[t], y + dy[t], t);
if (A[x][y] == 'Z') {
int s = (t ^ 1);
dfs(x + dx[s], y + dy[s], s);
} else {
int s = (t ^ 1 ^ 2);
dfs(x + dx[s], y + dy[s], s);
}
inq[x][y] = false;
});
vector<vector<int>> ans(N + 2, vector<int>(M + 2, INF));
for (int t : {1, 3}) {
for (int i = N; i >= 1; i--) {
for (int j = 1; j <= N; j++) {
fill(vis[j].begin(), vis[j].end(), false);
fill(inq[j].begin(), inq[j].end(), false);
}
time = 0;
if (t == 3) {
for (int j = 1; j <= M; j++) {
dfs(i, j, t);
ans[i][j] = min(ans[i][j], time);
}
} else {
for (int j = M; j >= 1; j--) {
dfs(i, j, t);
ans[i][j] = min(ans[i][j], time);
}
}
}
}
for (int i = 1; i <= N; i++)
for (int j = 1; j <= M; j++)
cout << (ans[i][j] >= INF ? -1 : ans[i][j] * 2) << " \n"[j == M];
}
Compilation message
sandwich.cpp:4: warning: ignoring '#pragma region y_combinator' [-Wunknown-pragmas]
4 | #pragma region y_combinator
|
sandwich.cpp:29: warning: ignoring '#pragma endregion y_combinator' [-Wunknown-pragmas]
29 | #pragma endregion y_combinator
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
232 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
300 KB |
Output is correct |
7 |
Correct |
6 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
308 KB |
Output is correct |
10 |
Correct |
7 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
304 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
300 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
304 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
232 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
300 KB |
Output is correct |
7 |
Correct |
6 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
308 KB |
Output is correct |
10 |
Correct |
7 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
344 KB |
Output is correct |
12 |
Correct |
2 ms |
212 KB |
Output is correct |
13 |
Correct |
7 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
340 KB |
Output is correct |
15 |
Correct |
4 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
304 KB |
Output is correct |
18 |
Correct |
3 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
340 KB |
Output is correct |
20 |
Correct |
3 ms |
340 KB |
Output is correct |
21 |
Correct |
5 ms |
212 KB |
Output is correct |
22 |
Correct |
3 ms |
300 KB |
Output is correct |
23 |
Correct |
4 ms |
340 KB |
Output is correct |
24 |
Correct |
6 ms |
304 KB |
Output is correct |
25 |
Correct |
5 ms |
340 KB |
Output is correct |
26 |
Correct |
5 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
28 |
Correct |
4 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
7 ms |
340 KB |
Output is correct |
31 |
Correct |
66 ms |
1964 KB |
Output is correct |
32 |
Correct |
76 ms |
1996 KB |
Output is correct |
33 |
Correct |
27 ms |
820 KB |
Output is correct |
34 |
Correct |
1581 ms |
2272 KB |
Output is correct |
35 |
Correct |
1222 ms |
1784 KB |
Output is correct |
36 |
Correct |
39 ms |
448 KB |
Output is correct |
37 |
Correct |
1719 ms |
2320 KB |
Output is correct |
38 |
Correct |
1543 ms |
2292 KB |
Output is correct |
39 |
Correct |
1765 ms |
2164 KB |
Output is correct |
40 |
Correct |
1987 ms |
2512 KB |
Output is correct |
41 |
Correct |
685 ms |
1700 KB |
Output is correct |
42 |
Correct |
1882 ms |
2544 KB |
Output is correct |
43 |
Correct |
1975 ms |
2536 KB |
Output is correct |
44 |
Correct |
1867 ms |
2540 KB |
Output is correct |
45 |
Correct |
1449 ms |
2264 KB |
Output is correct |
46 |
Correct |
958 ms |
2268 KB |
Output is correct |
47 |
Correct |
1320 ms |
2300 KB |
Output is correct |
48 |
Correct |
715 ms |
1808 KB |
Output is correct |
49 |
Correct |
1050 ms |
2268 KB |
Output is correct |
50 |
Correct |
1145 ms |
2280 KB |
Output is correct |
51 |
Correct |
946 ms |
2268 KB |
Output is correct |
52 |
Correct |
924 ms |
2280 KB |
Output is correct |
53 |
Correct |
920 ms |
2380 KB |
Output is correct |
54 |
Correct |
882 ms |
2124 KB |
Output is correct |
55 |
Correct |
923 ms |
2272 KB |
Output is correct |
56 |
Correct |
917 ms |
2272 KB |
Output is correct |
57 |
Correct |
736 ms |
1840 KB |
Output is correct |
58 |
Correct |
1809 ms |
2532 KB |
Output is correct |
59 |
Correct |
1857 ms |
2644 KB |
Output is correct |
60 |
Correct |
1293 ms |
2028 KB |
Output is correct |
61 |
Correct |
1791 ms |
2532 KB |
Output is correct |
62 |
Correct |
1319 ms |
2340 KB |
Output is correct |
63 |
Correct |
1307 ms |
2548 KB |
Output is correct |