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
#define in pair<int, int>
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define INF (int)1e17
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL)
const int SMX = 3e3+5;
char grid[SMX][SMX];
int gud[SMX][SMX][2];//automatically set to zero anyway
int solve(int DIAG, int l, int r)
{
int n = r-l+1;
int dp[n+1][2];
dp[0][0] = dp[0][1] = 0;
dp[1][0] = gud[l][DIAG-l][0]; dp[1][1] = gud[l][DIAG-l][1];
for(int i = 2; i <= n; i++)
{
dp[i][0] = gud[l-1+i][DIAG-l+1-i][0]+max(dp[i-1][0], max(dp[i-2][0], dp[i-2][1]));
dp[i][1] = gud[l-1+i][DIAG-l+1-i][1]+max(dp[i-1][1], max(dp[i-2][0], dp[i-2][1]));
}
int ans = 0;
for(int i = 0; i <= n; i++)
ans = max(ans, max(dp[i][0], dp[i][1]));
return ans;
}
signed main()
{
fast();
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
cin >> grid[i][j];
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(grid[i][j] != 'G')
continue;
if((i > 0) && (i < (n-1)) && (grid[i-1][j] == 'R') && (grid[i+1][j] == 'W'))
gud[i][j][0] = 1;
if((j > 0) && (j < (m-1)) && (grid[i][j-1] == 'R') && (grid[i][j+1] == 'W'))
gud[i][j][1] = 1;
}
}
int ans = 0;
int l[n+m-1], r[n+m-1];
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
r[i+j] = i;
}
for(int i = n-1; i >= 0; i--)
{
for(int j = 0; j < m; j++)
l[i+j] = i;
}
for(int DIAG = 0; DIAG <= n+m-2; DIAG++)
ans+=solve(DIAG, l[DIAG], r[DIAG]);
cout << ans << "\n";
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... |