#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
const int inf = 1e9;
mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );
int n, m;
vector<vector<int> > a;
bool valid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; }
array<int, 4> dx{ -1, 0, 1, 0 }, dy{ 0, -1, 0, 1 };
vector<vector<int> > f;
int F(int x, int y, int dir) {
if (!valid(x, y) ) return 0;
if (dir & 1) dir ^= a[x][y];
if (~f[x][y]) return f[x][y] ^ inf ? 0 : inf;
f[x][y] = inf;
int tmp1 = F(x + dx[dir], y + dy[dir], dir),
tmp2 = F(x + dx[dir ^ a[x][y] ], y + dy[dir ^ a[x][y] ], dir ^ a[x][y]);
if (tmp1 ^ inf && tmp2 ^ inf) f[x][y] = 2 + tmp1 + tmp2;
return f[x][y];
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafCLover
cin >> n >> m;
a.assign(n, vector<int>(m) );
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
char c; cin >> c;
a[i][j] = c == 'N' ? 3 : 1;
}
}
vector<vector<int> > ans(n, vector<int>(m, inf) );
for (int j = 0; j < m; ++j) {
for (const auto &dir : array<int, 2>{ 0, 2 }) {
int sum = 0;
f.assign(n, vector<int>(m, -1) );
if (!dir) {
for (int i = 0; i < n; ++i) {
int tmp = F(i, j, dir);
if (tmp == inf) break ;
sum += tmp;
asMn(ans[i][j], sum);
}
}
else {
for (int i = n - 1; ~i; --i) {
int tmp = F(i, j, dir);
if (tmp == inf) break ;
sum += tmp;
asMn(ans[i][j], sum);
}
}
}
}
for (const auto &i : ans) {
for (const auto &j : i) cout << (j ^ inf ? j : -1) << ' ';
cout << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
504 KB |
Output is correct |
7 |
Correct |
9 ms |
376 KB |
Output is correct |
8 |
Correct |
9 ms |
376 KB |
Output is correct |
9 |
Correct |
7 ms |
376 KB |
Output is correct |
10 |
Correct |
9 ms |
376 KB |
Output is correct |
11 |
Correct |
9 ms |
376 KB |
Output is correct |
12 |
Correct |
7 ms |
376 KB |
Output is correct |
13 |
Correct |
9 ms |
376 KB |
Output is correct |
14 |
Correct |
9 ms |
376 KB |
Output is correct |
15 |
Correct |
8 ms |
376 KB |
Output is correct |
16 |
Correct |
7 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
376 KB |
Output is correct |
18 |
Correct |
7 ms |
376 KB |
Output is correct |
19 |
Correct |
9 ms |
376 KB |
Output is correct |
20 |
Correct |
8 ms |
376 KB |
Output is correct |
21 |
Correct |
7 ms |
376 KB |
Output is correct |
22 |
Correct |
7 ms |
376 KB |
Output is correct |
23 |
Correct |
8 ms |
376 KB |
Output is correct |
24 |
Correct |
9 ms |
376 KB |
Output is correct |
25 |
Correct |
10 ms |
380 KB |
Output is correct |
26 |
Correct |
9 ms |
504 KB |
Output is correct |
27 |
Correct |
10 ms |
376 KB |
Output is correct |
28 |
Correct |
10 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
9 ms |
504 KB |
Output is correct |
7 |
Correct |
9 ms |
376 KB |
Output is correct |
8 |
Correct |
9 ms |
376 KB |
Output is correct |
9 |
Correct |
7 ms |
376 KB |
Output is correct |
10 |
Correct |
9 ms |
376 KB |
Output is correct |
11 |
Correct |
9 ms |
376 KB |
Output is correct |
12 |
Correct |
7 ms |
376 KB |
Output is correct |
13 |
Correct |
9 ms |
376 KB |
Output is correct |
14 |
Correct |
9 ms |
376 KB |
Output is correct |
15 |
Correct |
8 ms |
376 KB |
Output is correct |
16 |
Correct |
7 ms |
376 KB |
Output is correct |
17 |
Correct |
7 ms |
376 KB |
Output is correct |
18 |
Correct |
7 ms |
376 KB |
Output is correct |
19 |
Correct |
9 ms |
376 KB |
Output is correct |
20 |
Correct |
8 ms |
376 KB |
Output is correct |
21 |
Correct |
7 ms |
376 KB |
Output is correct |
22 |
Correct |
7 ms |
376 KB |
Output is correct |
23 |
Correct |
8 ms |
376 KB |
Output is correct |
24 |
Correct |
9 ms |
376 KB |
Output is correct |
25 |
Correct |
10 ms |
380 KB |
Output is correct |
26 |
Correct |
9 ms |
504 KB |
Output is correct |
27 |
Correct |
10 ms |
376 KB |
Output is correct |
28 |
Correct |
10 ms |
376 KB |
Output is correct |
29 |
Correct |
9 ms |
376 KB |
Output is correct |
30 |
Correct |
5 ms |
376 KB |
Output is correct |
31 |
Correct |
2503 ms |
8056 KB |
Output is correct |
32 |
Correct |
2522 ms |
7864 KB |
Output is correct |
33 |
Correct |
193 ms |
2424 KB |
Output is correct |
34 |
Correct |
1508 ms |
3320 KB |
Output is correct |
35 |
Correct |
1312 ms |
2684 KB |
Output is correct |
36 |
Correct |
6 ms |
504 KB |
Output is correct |
37 |
Correct |
1845 ms |
3448 KB |
Output is correct |
38 |
Correct |
1768 ms |
3448 KB |
Output is correct |
39 |
Correct |
1756 ms |
3320 KB |
Output is correct |
40 |
Correct |
2256 ms |
3592 KB |
Output is correct |
41 |
Correct |
1568 ms |
2412 KB |
Output is correct |
42 |
Correct |
2017 ms |
4000 KB |
Output is correct |
43 |
Correct |
1935 ms |
3832 KB |
Output is correct |
44 |
Correct |
1921 ms |
3836 KB |
Output is correct |
45 |
Correct |
1487 ms |
3448 KB |
Output is correct |
46 |
Correct |
1029 ms |
3452 KB |
Output is correct |
47 |
Correct |
1164 ms |
3280 KB |
Output is correct |
48 |
Correct |
673 ms |
2684 KB |
Output is correct |
49 |
Correct |
999 ms |
3392 KB |
Output is correct |
50 |
Correct |
1164 ms |
3376 KB |
Output is correct |
51 |
Correct |
1022 ms |
3320 KB |
Output is correct |
52 |
Correct |
1069 ms |
3320 KB |
Output is correct |
53 |
Correct |
1010 ms |
3320 KB |
Output is correct |
54 |
Correct |
973 ms |
3324 KB |
Output is correct |
55 |
Correct |
1022 ms |
3320 KB |
Output is correct |
56 |
Correct |
1050 ms |
3448 KB |
Output is correct |
57 |
Correct |
714 ms |
2808 KB |
Output is correct |
58 |
Correct |
2096 ms |
4344 KB |
Output is correct |
59 |
Correct |
2126 ms |
4168 KB |
Output is correct |
60 |
Correct |
1459 ms |
3444 KB |
Output is correct |
61 |
Correct |
2258 ms |
8440 KB |
Output is correct |
62 |
Correct |
2632 ms |
3448 KB |
Output is correct |
63 |
Correct |
2703 ms |
3576 KB |
Output is correct |