#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int N = 6e3 + 5;
int n, m, diagonals;
char a[N][N];
int dp[N][3];
bool canTake[N][N][3];
/*
1 -> up to down
2 -> left to right
*/
bool up(int i, int j) {
if(i <= 1 || i >= n) {
return false;
}
if(a[i - 1][j] != 'R' || a[i][j] != 'G' || a[i + 1][j] != 'W') {
return false;
}
return true;
}
bool left(int i, int j) {
if(j <= 1 || j >= m) {
return false;
}
if(a[i][j - 1] != 'R' || a[i][j] != 'G' || a[i][j + 1] != 'W') {
return false;
}
return true;
}
int Recur(int row, int prev) {
//cout << "Recur(" << row << ", " << prev << ")\n";
if(row > n) {
//cout << "Row already exceeded\n";
return 0;
}
if(dp[row][prev] != -1) {
//cout << "Dp already initialized = " << dp[row][prev] << "\n";
return dp[row][prev];
}
int col = diagonals - row;
int ans = Recur(row + 1, 0); //not choosing anything here :sadge:
if(prev == 0) {
//can choose both
if(canTake[row][col][1]) {
ans = max(ans, Recur(row + 1, 1) + 1);
}
if(canTake[row][col][2]) {
ans = max(ans, Recur(row + 1, 2) + 1);
}
}
else if(prev == 1) {
//can't choose left to right
if(canTake[row][col][1]) {
ans = max(ans, Recur(row + 1, 1) + 1);
}
}
else {
//can't choose up to down
if(canTake[row][col][2]) {
ans = max(ans, Recur(row + 1, 2) + 1);
}
}
return dp[row][prev] = ans;
}
void Solve() {
cin >> n >> m;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
canTake[i][j][0] = true;
canTake[i][j][1] = up(i, j);
canTake[i][j][2] = left(i, j);
}
}
int ans = 0;
for(diagonals = 2; diagonals <= n + m; diagonals++) {
for(int i = 0; i < N; i++) {
for(int j = 0; j < 3; j++) {
dp[i][j] = -1;
}
}
ans += Recur(1, 0); //row, previous taken...if any
/*for(int i = 1; i <= n; i++) {
for(int j = 0; j < 3; j++) {
cout << dp[i][j] << " ";
}
cout << "\n";
}*/
}
cout << ans << "\n";
}
int32_t main() {
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--) {
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
460 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
460 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
33 |
Correct |
1 ms |
460 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
460 KB |
Output is correct |
36 |
Correct |
1 ms |
460 KB |
Output is correct |
37 |
Correct |
1 ms |
460 KB |
Output is correct |
38 |
Correct |
1 ms |
460 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
1 ms |
460 KB |
Output is correct |
41 |
Correct |
1 ms |
460 KB |
Output is correct |
42 |
Correct |
1 ms |
460 KB |
Output is correct |
43 |
Correct |
1 ms |
460 KB |
Output is correct |
44 |
Correct |
1 ms |
460 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
1 ms |
512 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
460 KB |
Output is correct |
54 |
Correct |
25 ms |
460 KB |
Output is correct |
55 |
Correct |
197 ms |
24780 KB |
Output is correct |
56 |
Correct |
25 ms |
460 KB |
Output is correct |
57 |
Correct |
96 ms |
16740 KB |
Output is correct |
58 |
Correct |
85 ms |
12464 KB |
Output is correct |
59 |
Correct |
687 ms |
56736 KB |
Output is correct |
60 |
Correct |
685 ms |
63792 KB |
Output is correct |
61 |
Correct |
688 ms |
63800 KB |
Output is correct |
62 |
Correct |
24 ms |
460 KB |
Output is correct |
63 |
Correct |
671 ms |
62908 KB |
Output is correct |
64 |
Correct |
741 ms |
63812 KB |
Output is correct |
65 |
Correct |
714 ms |
63788 KB |
Output is correct |
66 |
Correct |
724 ms |
63860 KB |
Output is correct |
67 |
Correct |
665 ms |
63784 KB |
Output is correct |
68 |
Correct |
674 ms |
63848 KB |
Output is correct |
69 |
Correct |
654 ms |
63784 KB |
Output is correct |
70 |
Correct |
77 ms |
13380 KB |
Output is correct |
71 |
Correct |
75 ms |
13452 KB |
Output is correct |
72 |
Correct |
75 ms |
13448 KB |
Output is correct |
73 |
Correct |
78 ms |
13448 KB |
Output is correct |
74 |
Correct |
77 ms |
13452 KB |
Output is correct |
75 |
Correct |
77 ms |
13404 KB |
Output is correct |
76 |
Correct |
78 ms |
13380 KB |
Output is correct |
77 |
Correct |
77 ms |
13360 KB |
Output is correct |
78 |
Correct |
81 ms |
13524 KB |
Output is correct |
79 |
Correct |
75 ms |
13444 KB |
Output is correct |
80 |
Correct |
77 ms |
13368 KB |
Output is correct |
81 |
Correct |
78 ms |
13448 KB |
Output is correct |
82 |
Correct |
76 ms |
13388 KB |
Output is correct |
83 |
Correct |
80 ms |
13436 KB |
Output is correct |
84 |
Correct |
80 ms |
13448 KB |
Output is correct |
85 |
Correct |
75 ms |
13332 KB |
Output is correct |
86 |
Correct |
75 ms |
13356 KB |
Output is correct |
87 |
Correct |
78 ms |
13408 KB |
Output is correct |
88 |
Correct |
77 ms |
13380 KB |
Output is correct |
89 |
Correct |
76 ms |
13444 KB |
Output is correct |
90 |
Correct |
75 ms |
13452 KB |
Output is correct |