This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n, m;
cin >> n >> m;
m++, n++;
string s[n];
for (int i = 1; i < n; i++)
cin >> s[i], s[i] = " " + s[i];
int dp[n][m][3] = {};
for (int i = 3; i < n; i++)
dp[i][1][1] = dp[i - 1][1][1] + (s[i][1] == 'W' && s[i - 1][1] == 'G' && s[i - 2][1] == 'R'), dp[i][1][0] = dp[i - 1][1][1];
for (int j = 3; j < m; j++)
dp[1][j][2] = dp[1][j - 1][2] + (s[1][j] == 'W' && s[1][j - 1] == 'G' && s[1][j - 2] == 'R'), dp[1][j][0] = dp[1][j - 1][2];
for (int i = 2; i < n; i++) {
for (int j = 2; j < m; j++) {
dp[i][j][0] = max(dp[i - 1][j][1], dp[i - 2][j][0]) + max({dp[i][j - 1][0], dp[i][j - 1][1], dp[i][j - 1][2]}) - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]});
dp[i][j][0] = max(dp[i][j][0], max(dp[i - 1][j][2], dp[i - 1][j][0]) + dp[i][j - 1][2] - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]}));
dp[i][j][1] = max(dp[i - 1][j][1], dp[i - 2][j][0]) + max({dp[i][j - 1][0], dp[i][j - 1][1], dp[i][j - 1][2]}) - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]}) + (s[i][j] == 'W' && s[i - 1][j] == 'G' && s[i - 2][j] == 'R');
dp[i][j][1] = max(dp[i][j][1], max(dp[i - 2][j][2], dp[i - 1][j][0]) + dp[i][j - 1][2] - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]}) + (s[i][j] == 'W' && s[i - 1][j] == 'G' && s[i - 2][j] == 'R'));
dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 2][0]) + max({dp[i - 1][j][0], dp[i - 1][j][1], dp[i - 1][j][2]}) - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]}) + (s[i][j] == 'W' && s[i][j - 1] == 'G' && s[i][j - 2] == 'R');
dp[i][j][2] = max(dp[i][j][2], max(dp[i][j - 1][0], dp[i][j - 2][1]) + dp[i - 1][j][1] - max({dp[i - 1][j - 1][0], dp[i - 1][j - 1][1], dp[i - 1][j - 1][2]}) + (s[i][j] == 'W' && s[i][j - 1] == 'G' && s[i][j - 2] == 'R'));
}
}
cout << max({dp[n - 1][m - 1][0], dp[n - 1][m - 1][1], dp[n - 1][m - 1][2]});
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |