#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME (1.0 * clock() / CLOCKS_PER_SEC)
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3000 + 5;
const int oo = 1e18, mod = 1e9 + 7;
int n, m, k, ans=0, dp[2*N][5];
int a[N][N]; // R G W
void process() {
foru(tmp, 1, n+m) {
memset(dp, 0, sizeof(dp));
int i=1, j=tmp-1, cnt=1;
while (i<=n && j>=1) {
if (j>m) {
i++; j--; continue;
}
dp[cnt][0]=max(dp[cnt-1][0], max(dp[cnt-1][1], dp[cnt-1][2]));
if (a[i][j-1]==0 && a[i][j]==1 && a[i][j+1]==2) dp[cnt][1]=max(dp[cnt-1][0], dp[cnt-1][1])+1;
if (a[i-1][j]==0 && a[i][j]==1 && a[i+1][j]==2) dp[cnt][2]=max(dp[cnt-1][0], dp[cnt-1][2])+1;
i++; j--;
cnt++;
}
cnt--;
ans+=max(dp[cnt][0], max(dp[cnt][1], dp[cnt][2]));
//cout << dp[cnt][0] << ' ' << dp[cnt][1] << ' ' << dp[cnt][2] << '\n';
}
cout << ans << '\n';
return;
}
signed main() {
cin.tie(0)->sync_with_stdio(false);
//freopen(".inp", "r", stdin);
//freopen(".out", "w", stdout);
cin >> n >> m;
memset(a, -1, sizeof(a));
foru(i, 1, n) foru(j, 1, m) {
char x;
cin >> x;
if (x == 'R') a[i][j]=0;
else if (x=='G') a[i][j]=1;
else if (x=='W') a[i][j]=2;
}
process();
cerr << "Time elapsed: " << __TIME << " s.\n";
return 0;
}
/*
Xét các trường hợp đặc biệt
Kiểm tra lại input/output
Cố gắng trâu
Lật ngược bài toán
Calm down and get VOI
Stream of thought:
dp
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
71256 KB |
Output is correct |
2 |
Correct |
12 ms |
71256 KB |
Output is correct |
3 |
Correct |
9 ms |
71260 KB |
Output is correct |
4 |
Correct |
9 ms |
71132 KB |
Output is correct |
5 |
Correct |
9 ms |
71260 KB |
Output is correct |
6 |
Correct |
9 ms |
71324 KB |
Output is correct |
7 |
Correct |
10 ms |
71260 KB |
Output is correct |
8 |
Correct |
9 ms |
71260 KB |
Output is correct |
9 |
Correct |
9 ms |
71260 KB |
Output is correct |
10 |
Correct |
9 ms |
71180 KB |
Output is correct |
11 |
Correct |
9 ms |
71260 KB |
Output is correct |
12 |
Correct |
9 ms |
71332 KB |
Output is correct |
13 |
Correct |
9 ms |
71260 KB |
Output is correct |
14 |
Correct |
9 ms |
71260 KB |
Output is correct |
15 |
Correct |
10 ms |
71260 KB |
Output is correct |
16 |
Correct |
10 ms |
71260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
71256 KB |
Output is correct |
2 |
Correct |
12 ms |
71256 KB |
Output is correct |
3 |
Correct |
9 ms |
71260 KB |
Output is correct |
4 |
Correct |
9 ms |
71132 KB |
Output is correct |
5 |
Correct |
9 ms |
71260 KB |
Output is correct |
6 |
Correct |
9 ms |
71324 KB |
Output is correct |
7 |
Correct |
10 ms |
71260 KB |
Output is correct |
8 |
Correct |
9 ms |
71260 KB |
Output is correct |
9 |
Correct |
9 ms |
71260 KB |
Output is correct |
10 |
Correct |
9 ms |
71180 KB |
Output is correct |
11 |
Correct |
9 ms |
71260 KB |
Output is correct |
12 |
Correct |
9 ms |
71332 KB |
Output is correct |
13 |
Correct |
9 ms |
71260 KB |
Output is correct |
14 |
Correct |
9 ms |
71260 KB |
Output is correct |
15 |
Correct |
10 ms |
71260 KB |
Output is correct |
16 |
Correct |
10 ms |
71260 KB |
Output is correct |
17 |
Correct |
9 ms |
71260 KB |
Output is correct |
18 |
Correct |
9 ms |
71260 KB |
Output is correct |
19 |
Correct |
9 ms |
71164 KB |
Output is correct |
20 |
Correct |
9 ms |
71260 KB |
Output is correct |
21 |
Correct |
10 ms |
71260 KB |
Output is correct |
22 |
Correct |
12 ms |
71260 KB |
Output is correct |
23 |
Correct |
10 ms |
71260 KB |
Output is correct |
24 |
Correct |
9 ms |
71260 KB |
Output is correct |
25 |
Correct |
9 ms |
71376 KB |
Output is correct |
26 |
Correct |
9 ms |
71140 KB |
Output is correct |
27 |
Correct |
9 ms |
71260 KB |
Output is correct |
28 |
Correct |
10 ms |
71260 KB |
Output is correct |
29 |
Correct |
10 ms |
71260 KB |
Output is correct |
30 |
Correct |
9 ms |
71356 KB |
Output is correct |
31 |
Correct |
9 ms |
71260 KB |
Output is correct |
32 |
Correct |
10 ms |
71368 KB |
Output is correct |
33 |
Correct |
9 ms |
71260 KB |
Output is correct |
34 |
Correct |
9 ms |
71180 KB |
Output is correct |
35 |
Correct |
9 ms |
71260 KB |
Output is correct |
36 |
Correct |
9 ms |
71380 KB |
Output is correct |
37 |
Correct |
9 ms |
71260 KB |
Output is correct |
38 |
Correct |
9 ms |
71260 KB |
Output is correct |
39 |
Correct |
9 ms |
71260 KB |
Output is correct |
40 |
Correct |
9 ms |
71380 KB |
Output is correct |
41 |
Correct |
9 ms |
71148 KB |
Output is correct |
42 |
Correct |
9 ms |
71260 KB |
Output is correct |
43 |
Correct |
9 ms |
71260 KB |
Output is correct |
44 |
Correct |
10 ms |
71260 KB |
Output is correct |
45 |
Correct |
9 ms |
71260 KB |
Output is correct |
46 |
Correct |
9 ms |
71260 KB |
Output is correct |
47 |
Correct |
9 ms |
71260 KB |
Output is correct |
48 |
Correct |
10 ms |
71260 KB |
Output is correct |
49 |
Correct |
9 ms |
71164 KB |
Output is correct |
50 |
Correct |
9 ms |
71260 KB |
Output is correct |
51 |
Correct |
10 ms |
71260 KB |
Output is correct |
52 |
Correct |
9 ms |
71260 KB |
Output is correct |
53 |
Correct |
9 ms |
71260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
71256 KB |
Output is correct |
2 |
Correct |
12 ms |
71256 KB |
Output is correct |
3 |
Correct |
9 ms |
71260 KB |
Output is correct |
4 |
Correct |
9 ms |
71132 KB |
Output is correct |
5 |
Correct |
9 ms |
71260 KB |
Output is correct |
6 |
Correct |
9 ms |
71324 KB |
Output is correct |
7 |
Correct |
10 ms |
71260 KB |
Output is correct |
8 |
Correct |
9 ms |
71260 KB |
Output is correct |
9 |
Correct |
9 ms |
71260 KB |
Output is correct |
10 |
Correct |
9 ms |
71180 KB |
Output is correct |
11 |
Correct |
9 ms |
71260 KB |
Output is correct |
12 |
Correct |
9 ms |
71332 KB |
Output is correct |
13 |
Correct |
9 ms |
71260 KB |
Output is correct |
14 |
Correct |
9 ms |
71260 KB |
Output is correct |
15 |
Correct |
10 ms |
71260 KB |
Output is correct |
16 |
Correct |
10 ms |
71260 KB |
Output is correct |
17 |
Correct |
9 ms |
71260 KB |
Output is correct |
18 |
Correct |
9 ms |
71260 KB |
Output is correct |
19 |
Correct |
9 ms |
71164 KB |
Output is correct |
20 |
Correct |
9 ms |
71260 KB |
Output is correct |
21 |
Correct |
10 ms |
71260 KB |
Output is correct |
22 |
Correct |
12 ms |
71260 KB |
Output is correct |
23 |
Correct |
10 ms |
71260 KB |
Output is correct |
24 |
Correct |
9 ms |
71260 KB |
Output is correct |
25 |
Correct |
9 ms |
71376 KB |
Output is correct |
26 |
Correct |
9 ms |
71140 KB |
Output is correct |
27 |
Correct |
9 ms |
71260 KB |
Output is correct |
28 |
Correct |
10 ms |
71260 KB |
Output is correct |
29 |
Correct |
10 ms |
71260 KB |
Output is correct |
30 |
Correct |
9 ms |
71356 KB |
Output is correct |
31 |
Correct |
9 ms |
71260 KB |
Output is correct |
32 |
Correct |
10 ms |
71368 KB |
Output is correct |
33 |
Correct |
9 ms |
71260 KB |
Output is correct |
34 |
Correct |
9 ms |
71180 KB |
Output is correct |
35 |
Correct |
9 ms |
71260 KB |
Output is correct |
36 |
Correct |
9 ms |
71380 KB |
Output is correct |
37 |
Correct |
9 ms |
71260 KB |
Output is correct |
38 |
Correct |
9 ms |
71260 KB |
Output is correct |
39 |
Correct |
9 ms |
71260 KB |
Output is correct |
40 |
Correct |
9 ms |
71380 KB |
Output is correct |
41 |
Correct |
9 ms |
71148 KB |
Output is correct |
42 |
Correct |
9 ms |
71260 KB |
Output is correct |
43 |
Correct |
9 ms |
71260 KB |
Output is correct |
44 |
Correct |
10 ms |
71260 KB |
Output is correct |
45 |
Correct |
9 ms |
71260 KB |
Output is correct |
46 |
Correct |
9 ms |
71260 KB |
Output is correct |
47 |
Correct |
9 ms |
71260 KB |
Output is correct |
48 |
Correct |
10 ms |
71260 KB |
Output is correct |
49 |
Correct |
9 ms |
71164 KB |
Output is correct |
50 |
Correct |
9 ms |
71260 KB |
Output is correct |
51 |
Correct |
10 ms |
71260 KB |
Output is correct |
52 |
Correct |
9 ms |
71260 KB |
Output is correct |
53 |
Correct |
9 ms |
71260 KB |
Output is correct |
54 |
Correct |
17 ms |
71260 KB |
Output is correct |
55 |
Correct |
20 ms |
71392 KB |
Output is correct |
56 |
Correct |
17 ms |
71396 KB |
Output is correct |
57 |
Correct |
16 ms |
71168 KB |
Output is correct |
58 |
Correct |
42 ms |
72276 KB |
Output is correct |
59 |
Correct |
284 ms |
80088 KB |
Output is correct |
60 |
Correct |
288 ms |
80172 KB |
Output is correct |
61 |
Correct |
293 ms |
80344 KB |
Output is correct |
62 |
Correct |
16 ms |
71260 KB |
Output is correct |
63 |
Correct |
283 ms |
79872 KB |
Output is correct |
64 |
Correct |
180 ms |
80076 KB |
Output is correct |
65 |
Correct |
177 ms |
80172 KB |
Output is correct |
66 |
Correct |
180 ms |
80076 KB |
Output is correct |
67 |
Correct |
171 ms |
80172 KB |
Output is correct |
68 |
Correct |
178 ms |
80084 KB |
Output is correct |
69 |
Correct |
181 ms |
80084 KB |
Output is correct |
70 |
Correct |
28 ms |
72276 KB |
Output is correct |
71 |
Correct |
31 ms |
72564 KB |
Output is correct |
72 |
Correct |
33 ms |
72284 KB |
Output is correct |
73 |
Correct |
29 ms |
72284 KB |
Output is correct |
74 |
Correct |
30 ms |
72160 KB |
Output is correct |
75 |
Correct |
29 ms |
72276 KB |
Output is correct |
76 |
Correct |
28 ms |
72284 KB |
Output is correct |
77 |
Correct |
28 ms |
72324 KB |
Output is correct |
78 |
Correct |
27 ms |
72416 KB |
Output is correct |
79 |
Correct |
29 ms |
72120 KB |
Output is correct |
80 |
Correct |
30 ms |
72276 KB |
Output is correct |
81 |
Correct |
35 ms |
72284 KB |
Output is correct |
82 |
Correct |
28 ms |
72276 KB |
Output is correct |
83 |
Correct |
28 ms |
72276 KB |
Output is correct |
84 |
Correct |
34 ms |
72276 KB |
Output is correct |
85 |
Correct |
28 ms |
72268 KB |
Output is correct |
86 |
Correct |
37 ms |
72160 KB |
Output is correct |
87 |
Correct |
29 ms |
72272 KB |
Output is correct |
88 |
Correct |
31 ms |
72276 KB |
Output is correct |
89 |
Correct |
27 ms |
72112 KB |
Output is correct |
90 |
Correct |
28 ms |
72284 KB |
Output is correct |