This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
const int N = 1E7+100, M = 1e5+10, K = 20;
int n, m;
string s[4000];
bitset<3> g[N][2];
bitset<N> v[2];
bitset<N> vis[2];
vector<vector<int>> arr;
int aa[3] = {1, 2, 0};
pair<int, int> dfs(int x, int p){
pair<int, int> ans = {1, 0};
vis[p][x] = 1;
for(int k = 0; k < 3; ++k){
int y = aa[k];
if(!g[x][p][y]) continue;
pair<int, int> u = {arr[p][y]+x, 1-p};
if(vis[u.second][u.first]) continue;
auto dp = dfs(u.first, u.second);
ans.first += dp.second;
ans.second += max(dp.first, dp.second);
}
// cout <<x/m << ' ' << x%m << ' ' << p << ' ' << ans.first << ' ' << ans.second << '\n';
return ans;
}
void solve(){
cin >> n >> m;
arr = {
{
0,
m-1,
2*m-2
},
{
0,
-m+1,
-2*m+2
}
};
for(int i = 0; i < n; ++i) cin >> s[i];
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(i < n - 2){
int x = i*m+j;
if(s[i][j] == 'R' && s[i+1][j] == 'G' && s[i+2][j] == 'W'){
if(v[1][x]) g[x][0][0] = 1, g[x][1][0] = 1;
if(v[1][x+m]) g[x][0][1] = 1, g[x+m-1][1][1] = 1;
if(v[1][x+m+m]) g[x][0][2] = 1, g[x+2*m-2][1][2] = 1;
v[0][x] = v[0][x+m] = v[0][x+m+m] = 1;
}
}
if(j < m-2){
int x = i*m+j;
if(s[i][j] == 'R' && s[i][j+1] == 'G' && s[i][j+2] == 'W'){
if(v[0][x]) g[x][1][0] = 1, g[x][0][0] = 1;
if(v[0][x+1]) g[x][1][1] = 1, g[x+1-m][0][1] = 1;
if(v[0][x+2]) g[x][1][2] = 1, g[x+2-2*m][0][2] = 1;
v[1][x] = v[1][x+1] = v[1][x+2] = 1;
}
}
}
}
int ans = 0;
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(s[i][j] != 'R') continue;
if(!vis[1][i*m+j] && v[1][i*m+j]){
auto dp = dfs(i*m+j, 1);
ans += max(dp.first, dp.second);
// cout << i << ' ' << j << ' '<< ' ' << 0 << ' ' << ans << '\n';
}else if(!vis[0][i*m+j] && v[0][i*m+j]){
auto dp = dfs(i*m+j, 0);
ans += max(dp.first, dp.second);
// cout << i << ' ' << j << ' '<< ' ' << 1 << ' ' << ans << '\n';
}
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
}
return 0;
}
Compilation message (stderr)
dango_maker.cpp: In function 'int main()':
dango_maker.cpp:98:14: warning: unused variable 'aa' [-Wunused-variable]
98 | int T = 1, aa;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |