#include <bits/stdc++.h>
#define MAX_N 3000
#define MAX_M 3000
using namespace std;
int a[MAX_N + 1][MAX_M + 1];
int lin[MAX_N + 1][MAX_M + 1];
int col[MAX_N + 1][MAX_M + 1];
int dp[MAX_N + 1][MAX_M + 1];
int n, m;
int rez;
string s;
void readFile()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> s;
for(int j = 1; j <= m; j ++)
a[i][j] = (s[j - 1] == 'R' ? 0 : (s[j - 1] == 'G' ? 1 : 2));
}
}
void getLin()
{
for(int i = 1; i <= n; i ++)
{
for(int j = 3; j <= m; j ++)
{
lin[i][j] = lin[i][j - 1];
if(a[i][j - 2] == 0 && a[i][j - 1] == 1 && a[i][j] == 2)
lin[i][j] = max(lin[i][j], lin[i][j - 3] + 1);
}
}
}
void getCol()
{
for(int j = 1; j <= m; j ++)
{
for(int i = 3; i <= n; i ++)
{
col[i][j] = col[i - 1][j];
if(a[i - 2][j] == 0 && a[i - 1][j] == 1 && a[i][j] == 2)
col[i][j] = max(col[i][j], col[i - 3][j] + 1);
}
}
}
void getDp()
{
for(int i = 1; i <= n; i ++)
dp[i][1] = col[i][1];
for(int j = 1; j <= m; j ++)
dp[1][j] = lin[1][j];
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
if(i + 1 <= n)
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + lin[i + 1][j]);
if(j + 1 <= m)
dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + col[i][j + 1]);
}
}
rez = dp[n][m];
}
void solve()
{
getLin();
getCol();
getDp();
}
void printFile()
{
cout << rez << "\n";
}
int main()
{
readFile();
solve();
printFile();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 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 |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |