#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= (int)b; i++)
#define FORD(i, a, b) for(int i = a; i >= (int)b; i--)
const int N = 3e5 + 5;
int n, m;
int a[3005][3005];
int dpRow[3005][3005][2], dpCol[3005][3005][2], dp[3005][3005];
int is_dango(int x, int y, int z) {
vector <int> res;
res.push_back(x); res.push_back(y); res.push_back(z);
sort(res.begin(), res.end());
return res[0] == 1 && res[1] == 2 && res[2] == 3;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define ko "kieuoanh"
if (fopen(ko".inp", "r")) {
freopen(ko".inp", "r", stdin);
freopen(ko".out", "w", stdout);
}
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char x; cin >> x;
if (x == 'R') a[i][j] = 1;
if (x == 'G') a[i][j] = 2;
if (x == 'W') a[i][j] = 3;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 3; j <= m; j++) {
dpRow[i][j][0] = max({dpRow[i][j - 1][1], dpRow[i][j - 1][0]});
if (is_dango(a[i][j - 2], a[i][j - 1], a[i][j])) {
dpRow[i][j][1] = max({dpRow[i][j - 3][1] + 1, dpRow[i][j - 3][0] + 1});
}
}
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
dpCol[i][j][0] = max({dpCol[i - 1][j][1], dpRow[i - 1][j][0]});
if (is_dango(a[i - 2][j], a[i - 1][j], a[i][j])) {
dpCol[i][j][1] = max({dpCol[i - 3][j][1] + 1, dpCol[i - 3][j][0] + 1});
}
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i >= 1) dp[i][j] = max(dp[i][j], dp[i - 1][j] + max(dpRow[i][j][0], dpRow[i][j][1]));
if (j >= 1) dp[i][j] = max(dp[i][j], dp[i][j - 1] + max(dpCol[i][j][0], dpCol[i][j][1]));
}
}
cout << dp[n][m];
cerr << "\n[Time Elapsed] " << 0.001 * clock() << "s\n";
return 0;
}
Compilation message (stderr)
dango_maker.cpp: In function 'int main()':
dango_maker.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(ko".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
dango_maker.cpp:24:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | freopen(ko".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |