#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |