#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <cstring>
#include <iterator>
#include <random>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll dim = 5*1e3+7;
//const ll mod = 1e9 + 7;
const ll inf = 1e18 + 77;
#define endl "\n"
#define fi first
#define pb push_back
#define se second
#define vll vector<ll>
ll n, m;
ll a[dim][dim],up[dim][dim], rt[dim][dim], dp[dim][dim][3];
int main() {
ll k, t, u0, v0;
string s1;
cin>>n>>m;
ll ans=0;
for(int i=1; i<=n; i++){
cin>>s1;
for(int j=1; j<=m; j++){
if(s1[j-1]=='R')a[i][j]=1;
else if(s1[j-1]=='G')a[i][j]=2;
else a[i][j]=3;
dp[i][j][0]=0;
dp[i][j][1]=0;
dp[i][j][2]=0;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(a[i][j-1]==1 && a[i][j]==2 && a[i][j+1]==3 && j<m) {
rt[i][j] = 1;
}
if(a[i-1][j]==1 && a[i][j]==2 && a[i+1][j]==3 && i<n){
up[i][j]=1;
}
}
}
for(int s=2; s<=n+m; s++){
for(int i=min<ll>(n, s-1); i>=1; i--){
ll j=s-i;
if(j==0 || j>m)continue;
if(up[i][s-i])dp[i][s-i][2]=max(dp[i+1][s-(i+1)][2],dp[i+1][s-(i+1)][0])+1;
if(rt[i][s-i])dp[i][s-i][1]=max(dp[i+1][s-(i+1)][1],dp[i+1][s-(i+1)][0])+1;
dp[i][s-i][0]=max({dp[i+1][s-(i+1)][1],dp[i+1][s-(i+1)][0], dp[i+1][s-(i+1)][2]});
}
ll endj=min<ll> (m, s-1);
ans+=max({dp[s-endj][endj][1], dp[s-endj][endj][2], dp[s-endj][endj][0]});
}
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... |