#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void test_case() {
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto &x : a) cin >> x;
vector<vector<bool>> vis(n, vector<bool>(m));
const int dx[][2] = {{1, 2}, {-1, 1}, {-1, -2}};
const string match = "RGW";
auto in = [&](int x, int y) {
return x>=0 && y>=0 && x<n && y<m;
};
auto check = [&](int x, int y, queue<ar<int, 2>> &v) {
int idx = a[x][y] == 'R' ? 0 : a[x][y] == 'G' ? 1 : 2;
bool ok = 1;
for (int d = 0; d < 2; d++) {
int nx = x + dx[idx][d];
if (!in(nx, y) || a[nx][y] != match[idx + dx[idx][d]]) {
ok = 0;
break;
}
}
if (ok) {
if (!vis[x][y]) v.push({x, y}), vis[x][y] = 1;
for (int d = 0; d < 2; d++) {
int nx = x + dx[idx][d];
if (!vis[nx][y]) {
vis[nx][y] = 1;
v.push({nx, y});
}
}
}
bool ok2 = 1;
for (int d = 0; d < 2; d++) {
int ny = y + dx[idx][d];
if (!in(x, ny) || a[x][ny] != match[idx + dx[idx][d]]) {
ok2 = 0;
break;
}
}
if (ok2) {
if (!vis[x][y]) v.push({x, y}), vis[x][y] = 1;
for (int d = 0; d < 2; d++) {
int ny = y + dx[idx][d];
if (!vis[x][ny]) {
v.push({x, ny});
vis[x][ny] = 1;
}
}
}
};
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) if (!vis[i][j]) {
queue<ar<int, 2>> q;
check(i, j, q);
vector<ar<int, 2>> cur;
while (q.size()) {
auto [x, y] = q.front();
cur.push_back(q.front());
q.pop();
check(x, y, q);
}
auto good = [&](int idx) {
auto [x, y] = cur[idx];
return x+2 < n && a[x][y] == 'R' && a[x+1][y] == 'G' && a[x+2][y] == 'W';
};
if (cur.size()) {
sort(all(cur));
vector<int> dp(8);
int L = -1, R = -1;
for (int k = 0; k < sz(cur); k++) {
int r = k;
while (r+1 < sz(cur) && cur[r+1][0] == cur[r][0]) r++;
int curl = cur[k][1], curr = cur[r][1];
if (L == -1) {
if (r-k+1 == 3) dp[0] = 1;
if (good(k)) dp[1] = 1;
}
else {
vector<int> dp2(8);
for (int mask = 0; mask < 8; mask++) {
int nw = 0;
for (int z = L; z <= R; z++) {
int idx = z - L;
if ((mask >> idx & 1) && (curl <= z && z <= curr)) {
nw |= 1 << (z - curl);
}
}
ckmax(dp2[nw], dp[mask]);
if (!(nw & 1) && good(k)) {
ckmax(dp2[nw | 1], dp[mask] + 1);
}
if (!nw && r-k+1 == 3) ckmax(dp2[nw], dp[mask] + 1);
}
swap(dp, dp2);
}
//for (int i = 0; i < 8; i++) cout << dp[i] << " \n"[i==7];
assert(r-k+1 <= 3);
L = curl, R = curr;
k = r;
}
/*
cout << "--------\n";
for (auto [x, y] : cur) cout << x << " " << y << '\n';
cout << *max_element(all(dp)) << "\n\n";
*/
ans += *max_element(all(dp));
}
}
}
cout << ans << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
344 KB |
Output is correct |
35 |
Correct |
0 ms |
600 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
344 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
1 ms |
716 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
2 ms |
604 KB |
Output is correct |
58 |
Correct |
62 ms |
2696 KB |
Output is correct |
59 |
Correct |
546 ms |
20284 KB |
Output is correct |
60 |
Correct |
547 ms |
20432 KB |
Output is correct |
61 |
Correct |
550 ms |
20288 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
534 ms |
19644 KB |
Output is correct |
64 |
Correct |
822 ms |
20496 KB |
Output is correct |
65 |
Correct |
778 ms |
20732 KB |
Output is correct |
66 |
Correct |
793 ms |
20476 KB |
Output is correct |
67 |
Correct |
909 ms |
20352 KB |
Output is correct |
68 |
Correct |
904 ms |
20512 KB |
Output is correct |
69 |
Correct |
896 ms |
20308 KB |
Output is correct |
70 |
Correct |
102 ms |
2648 KB |
Output is correct |
71 |
Correct |
84 ms |
2652 KB |
Output is correct |
72 |
Correct |
82 ms |
2560 KB |
Output is correct |
73 |
Correct |
86 ms |
2900 KB |
Output is correct |
74 |
Correct |
83 ms |
2736 KB |
Output is correct |
75 |
Correct |
83 ms |
2652 KB |
Output is correct |
76 |
Correct |
82 ms |
2748 KB |
Output is correct |
77 |
Correct |
86 ms |
2608 KB |
Output is correct |
78 |
Correct |
82 ms |
2652 KB |
Output is correct |
79 |
Correct |
84 ms |
2736 KB |
Output is correct |
80 |
Correct |
85 ms |
2756 KB |
Output is correct |
81 |
Correct |
98 ms |
2652 KB |
Output is correct |
82 |
Correct |
83 ms |
2648 KB |
Output is correct |
83 |
Correct |
85 ms |
2752 KB |
Output is correct |
84 |
Correct |
85 ms |
2652 KB |
Output is correct |
85 |
Correct |
83 ms |
2756 KB |
Output is correct |
86 |
Correct |
83 ms |
2652 KB |
Output is correct |
87 |
Correct |
82 ms |
2648 KB |
Output is correct |
88 |
Correct |
92 ms |
2652 KB |
Output is correct |
89 |
Correct |
83 ms |
2648 KB |
Output is correct |
90 |
Correct |
85 ms |
2908 KB |
Output is correct |