#include "bits/stdc++.h"
using namespace std;
using ll = long long;
// RGW
// template<class T>
// struct dinic {
// static constexpr T INF = numeric_limits<T>::max();
// struct edge {
// int v, r; T c;
// edge(int _v, int _r, T _c) : v{_v}, r{_r}, c{_c} {}
// };
// int n, s, t;
// vector<vector<edge>> G;
// vector<int> d, arc;
// dinic(int _n, int _s, int _t) : n{_n}, s{_s}, t{_t}, G(_n) {}
// void add_edge(int u, int v, T c) {
// G[u].emplace_back(v, (int)G[v].size(), c);
// G[v].emplace_back(u, (int)G[u].size() - 1, 0);
// }
// int bfs() {
// d.assign(n, 0), arc.assign(n, 0);
// queue<int> q;
// d[s] = 1, q.emplace(s);
// while (!q.empty()) {
// int u = q.front();
// q.pop();
// for (auto [v, r, c] : G[u]) {
// if (!d[v] && c) {
// d[v] = d[u] + 1;
// if (v == t) return 1;
// q.emplace(v);
// }
// }
// }
// return d[t];
// }
// T dfs(int u, T f) {
// if (u == t) return f;
// T sum = 0;
// for (int& i = arc[u]; i < (int)G[u].size(); ++i) {
// auto& [v, r, c] = G[u][i];
// if (d[v] == d[u] + 1 && c) {
// T res = dfs(v, min(f, c));
// if (res) {
// c -= res, G[v][r].c += res;
// sum += res, f -= res;
// if (!f) return sum;
// } else {
// d[v] = 0;
// }
// }
// }
// return sum;
// }
// T max_flow() {
// T sum = 0;
// while (bfs()) {
// while (T res = dfs(s, INF)) {
// sum += res;
// }
// }
// return sum;
// }
// };
// bool dfs(int cur) {
// for (int nxt : va[cur]) {
// if (!visitb[nxt]) {
// visitb[nxt] = true;
// clrS.push(nxt);
// if (mb[nxt] == 0 || dfs(mb[nxt])) {
// ma[cur] = nxt;
// mb[nxt] = cur;
// return true;
// }
// }
// }
// return false;
// }
// int bipartite_matching() {
// memset(ma, -1, sizeof(ma));
// memset(mb, -1, sizeof(mb));
// int ret = 0;
// for (int i = 1; i <= cnt; i++) {
// while (!clrS.empty()) {
// visitb[clrS.top()] = false;
// clrS.pop();
// }
// if (dfs(i)) ++ret;
// }
// return ret;
// }
int dp[6005][3005][2][2];
string s[3005];
int main() {
int n, m;
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int ans = 0;
for (int k = 0; k < n + m - 1; k++) {
for (int i = max(0, k - m + 1); i < min(n, k + 1); i++) {
int j = k - i;
if (i == 0) {
dp[k][i][0][0] = 0;
}
else {
dp[k][i][0][0] = max(dp[k][i][0][0], dp[k][i - 1][0][0]);
dp[k][i][0][0] = max(dp[k][i][0][0], dp[k][i - 1][0][1]);
dp[k][i][0][0] = max(dp[k][i][0][0], dp[k][i - 1][1][0]);
}
if (s[i][j] != 'G') continue;
if (j - 1 >= 0 && j + 1 < m && s[i][j - 1] == 'R' && s[i][j + 1] == 'W') {
if (i == 0) {
dp[k][i][1][0] = 1;
}
else {
dp[k][i][1][0] = max(dp[k][i][1][0], dp[k][i - 1][0][0] + 1);
dp[k][i][1][0] = max(dp[k][i][1][0], dp[k][i - 1][1][0] + 1);
}
}
if (i - 1 >= 0 && i + 1 < n && s[i - 1][j] == 'R' && s[i + 1][j] == 'W') {
if (i == 0) {
dp[k][i][0][1] = 1;
}
else {
dp[k][i][0][1] = max(dp[k][i][0][1], dp[k][i - 1][0][0] + 1);
dp[k][i][0][1] = max(dp[k][i][0][1], dp[k][i - 1][0][1] + 1);
}
}
}
int cur = 0;
cur = max(cur, dp[k][min(n, k + 1) - 1][0][0]);
cur = max(cur, dp[k][min(n, k + 1) - 1][0][1]);
cur = max(cur, dp[k][min(n, k + 1) - 1][1][0]);
ans += cur;
}
cout << ans << endl;
// int cnt, cnt2;
// cnt = cnt2 = 0;
// vector a(n, vector<int>(n));
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// if (s[i][j] != 'R') continue;
// if (j + 2 < m) {
// if (s[i][j + 1] == 'G' && s[i][j + 2] == 'W') {
// ++cnt;
// // a[i][j] = cnt;
// // a[i][j + 1] = cnt;
// // a[i][j + 2] = cnt;
// }
// }
// if (i + 2 < n) {
// if (s[i + 1][j] == 'G' && s[i + 2][j] == 'W') {
// ++cnt2;
// // b[i][j]= cnt2;
// // b[i + 1][j]= cnt2;
// // b[i + 2][j]= cnt2;
// }
// }
// }
// }
// return 0;
// dinic<int> D(cnt + cnt2 + 2, 0, 1);
// for (int i = 1; i <= cnt; i++) {
// D.add_edge(0, i + 1, 1);
// }
// for (int i = 1; i <= cnt2; i++) {
// D.add_edge(cnt + i + 1, 1, 1);
// }
// int cnt3 = 0;
// int cnt4 = 0;
// for (int k = 0; k < n + m - 1; k++) {
// int prevcnt = -200;
// int prevdex = -200;
// int prevcnt2 = -200;
// int prevdex2 = -200;
// for (int i = max(0, k - m + 1); i < min(n, k + 1); i++) {
// int j = k - i;
// if (s[i][j] != 'R') continue;
// if (j + 2 < m) {
// if (s[i][j + 1] == 'G' && s[i][j + 2] == 'W') {
// ++cnt3;
// if (prevdex == i - 1 || prevdex == i - 2) {
// D.add_edge(cnt3 + 1, cnt + prevcnt + 1, D.INF);
// // cout << prevdex << " -> " << i << endl;
// }
// if (prevdex2 == i - 2) {
// D.add_edge(cnt3 + 1, cnt + prevcnt2 + 1, D.INF);
// // cout << prevdex2 << " -> " << i << endl;
// }
// if (i + 2 < n && s[i + 1][j] == 'G' && s[i + 2][j] == 'W') {
// D.add_edge(cnt3 + 1, cnt + cnt4 + 1 + 1, D.INF);
// // cout << i << " -> " << i << endl;
// }
// // b[i][j]= cnt2;
// // b[i + 1][j]= cnt2;
// // b[i + 2][j]= cnt2;
// }
// }
// if (i + 2 < n) {
// if (s[i + 1][j] == 'G' && s[i + 2][j] == 'W') {
// ++cnt4;
// swap(prevcnt, prevcnt2);
// swap(prevdex, prevdex2);
// prevcnt = cnt3;
// prevdex = i;
// // a[i][j] = cnt;
// // a[i][j + 1] = cnt;
// // a[i][j + 2] = cnt;
// }
// }
// }
// }
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// if (a[i][j] != 0 && b[i][j] != 0) {
// D.add_edge(a[i][j] + 1, cnt + b[i][j] + 1, D.INF);
// // va[a[i][j]].push_back(b[i][j]);
// }
// }
// }
// cout << cnt + cnt2 - D.max_flow() << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
0 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
0 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
0 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
0 ms |
468 KB |
Output is correct |
53 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
0 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
0 ms |
468 KB |
Output is correct |
32 |
Correct |
0 ms |
468 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
0 ms |
468 KB |
Output is correct |
35 |
Correct |
0 ms |
468 KB |
Output is correct |
36 |
Correct |
1 ms |
468 KB |
Output is correct |
37 |
Correct |
1 ms |
468 KB |
Output is correct |
38 |
Correct |
0 ms |
468 KB |
Output is correct |
39 |
Correct |
0 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
468 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
0 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
0 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
468 KB |
Output is correct |
50 |
Correct |
0 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
0 ms |
468 KB |
Output is correct |
53 |
Correct |
0 ms |
468 KB |
Output is correct |
54 |
Correct |
5 ms |
12628 KB |
Output is correct |
55 |
Correct |
7 ms |
12628 KB |
Output is correct |
56 |
Correct |
5 ms |
12884 KB |
Output is correct |
57 |
Correct |
5 ms |
8848 KB |
Output is correct |
58 |
Correct |
25 ms |
25264 KB |
Output is correct |
59 |
Correct |
256 ms |
174188 KB |
Output is correct |
60 |
Correct |
209 ms |
174156 KB |
Output is correct |
61 |
Correct |
207 ms |
174228 KB |
Output is correct |
62 |
Correct |
6 ms |
11860 KB |
Output is correct |
63 |
Correct |
225 ms |
169124 KB |
Output is correct |
64 |
Correct |
174 ms |
182984 KB |
Output is correct |
65 |
Correct |
172 ms |
183072 KB |
Output is correct |
66 |
Correct |
176 ms |
183012 KB |
Output is correct |
67 |
Correct |
200 ms |
183028 KB |
Output is correct |
68 |
Correct |
201 ms |
183032 KB |
Output is correct |
69 |
Correct |
175 ms |
183008 KB |
Output is correct |
70 |
Correct |
20 ms |
26172 KB |
Output is correct |
71 |
Correct |
20 ms |
26120 KB |
Output is correct |
72 |
Correct |
22 ms |
26148 KB |
Output is correct |
73 |
Correct |
19 ms |
26188 KB |
Output is correct |
74 |
Correct |
25 ms |
26168 KB |
Output is correct |
75 |
Correct |
20 ms |
26208 KB |
Output is correct |
76 |
Correct |
20 ms |
26200 KB |
Output is correct |
77 |
Correct |
20 ms |
26184 KB |
Output is correct |
78 |
Correct |
21 ms |
26188 KB |
Output is correct |
79 |
Correct |
20 ms |
26192 KB |
Output is correct |
80 |
Correct |
21 ms |
26256 KB |
Output is correct |
81 |
Correct |
21 ms |
26180 KB |
Output is correct |
82 |
Correct |
21 ms |
26152 KB |
Output is correct |
83 |
Correct |
21 ms |
26132 KB |
Output is correct |
84 |
Correct |
21 ms |
26160 KB |
Output is correct |
85 |
Correct |
22 ms |
26196 KB |
Output is correct |
86 |
Correct |
20 ms |
26220 KB |
Output is correct |
87 |
Correct |
21 ms |
26144 KB |
Output is correct |
88 |
Correct |
21 ms |
26168 KB |
Output is correct |
89 |
Correct |
21 ms |
26140 KB |
Output is correct |
90 |
Correct |
20 ms |
26188 KB |
Output is correct |