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 <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef long long int llo;
#define a first
#define b second
#define endl "\n"
//vector<pair<pair<int,int>,int>> adj[3001][3001][2];
//int vis[3001][3001][2];
int dp[3001][3001][2][2];//1 take 0 not take
int it[3001][3001];
int n,m;
void dfs(int i,int j,int kk){
dp[i][j][kk][0]=0;
dp[i][j][kk][1]=0;
if(kk==0){
if(it[i][j]==0 and it[i+1][j]==1 and it[i+2][j]==2){
dp[i][j][kk][1]=1;
if(j<m-2 ){
if(dp[i][j][1-kk][0]==-1){
dfs(i,j,1-kk);
dp[i][j][kk][1]+=dp[i][j][1-kk][0];
dp[i][j][kk][0]+=dp[i][j][1-kk][1];
}
}
if(j<m-1 and j>0){
if(dp[i+1][j-1][1-kk][0]==-1){
dfs(i+1,j-1,1-kk);
dp[i][j][kk][1]+=dp[i+1][j-1][1-kk][0];
dp[i][j][kk][0]+=dp[i+1][j-1][1-kk][1];
}
}
if(j>1){
if(dp[i+2][j-2][1-kk][0]==-1){
dfs(i+2,j-2,1-kk);
dp[i][j][kk][1]+=dp[i+2][j-2][1-kk][0];
dp[i][j][kk][0]+=dp[i+2][j-2][1-kk][1];
}
}
}
}
if(kk==1){
if(it[i][j]==0 and it[i][j+1]==1 and it[i][j+2]==2){
dp[i][j][kk][1]=1;
//cout<<i<<" "<<j<<" "<<0<<endl;
if(i<n-2){
if(dp[i][j][1-kk][0]==-1){
dfs(i,j,1-kk);
dp[i][j][kk][1]+=dp[i][j][1-kk][0];
dp[i][j][kk][0]+=dp[i][j][1-kk][1];
}
}
if(i<n-1 and i>0){
if(dp[i-1][j+1][1-kk][0]==-1){
dfs(i-1,j+1,1-kk);
dp[i][j][kk][1]+=dp[i-1][j+1][1-kk][0];
dp[i][j][kk][0]+=dp[i-1][j+1][1-kk][1];
}
}
if(i>1){
if(dp[i-2][j+2][1-kk][0]==-1){
dfs(i-2,j+2,1-kk);
dp[i][j][kk][1]+=dp[i-2][j+2][1-kk][0];
dp[i][j][kk][0]+=dp[i-2][j+2][1-kk][1];
}
}
}
}
dp[i][j][kk][1]=max(dp[i][j][kk][1],dp[i][j][kk][0]);
}
int main(){
ios_base::sync_with_stdio(false);
// memset(vis,1,sizeof(vis));
cin.tie(NULL);
memset(dp,-1,sizeof(dp));
char s;
cin>>n>>m;
//int it[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>s;
if(s=='R'){
it[i][j]=0;
}
else if(s=='G'){
it[i][j]=1;
}
else{
it[i][j]=2;
}
// cout<<it[i][j]<<" ";
}
// cout<<endl;
}
/* int st=0;
for(int i=0;i<n;i++){
for(int j=0;j<m-2;j++){
if(it[i][j]==0 and it[i][j+1]==1 and it[i][j+2]==2){
vis[i][j][1]=0;
// cout<<i<<" "<<j<<" "<<1<<endl;
}
}
}*/
/* for(int i=0;i<n-2;i++){
for(int j=0;j<m;j++){
if(it[i][j]==0 and it[i+1][j]==1 and it[i+2][j]==2){
vis[i][j][0]=0;
//cout<<i<<" "<<j<<" "<<0<<endl;
if(j<m-2){
if(it[i][j+1]==1 and it[i][j+2]==2){
adj[i][j][0].pb(mp(mp(i,j),1));
adj[i][j][1].pb(mp(mp(i,j),0));
}
}
if(j<m-1 and j>0){
if(it[i+1][j-1]==0 and it[i+1][j+1]==2){
adj[i][j][0].pb(mp(mp(i+1,j-1),1));
adj[i+1][j-1][1].pb(mp(mp(i,j),0));
}
}
if(j>1){
if(it[i+2][j-2]==0 and it[i+2][j-1]==1){
adj[i][j][0].pb(mp(mp(i+2,j-2),1));
adj[i+2][j-2][1].pb(mp(mp(i,j),0));
}
}
}
}
}*/
int ans=0;
for(int ii=0;ii<n;ii++){
for(int jj=0;jj<m;jj++){
if(dp[ii][jj][0][0]==-1 and ii<n-2){
dfs(ii,jj,0);
ans+=dp[ii][jj][0][1];
}
if(dp[ii][jj][1][0]==-1 and jj<m-2){
dfs(ii,jj,1);
ans+=dp[ii][jj][1][1];
}
}
}
cout<<ans<<endl;
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... |