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;
const int MAXN = 3001;
char grid[MAXN][MAXN];
int mp[MAXN][MAXN][2];
vector<int> adj[MAXN*MAXN];
vector<bool> vis;
int side1 = 0, side2 = 0;
void dfs(int u, int s){
vis[u] = true;
if(s == 1){
++side1;
}
else{
++side2;
}
for(auto v : adj[u]){
if(!vis[v]){
dfs(v,3-s);
}
}
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N,M; cin >> N >> M;
for(int i = 0;i<N;++i){
for(int j = 0;j<M;++j){
cin >> grid[i][j];
}
}
int n = 0;
for(int i = 0;i<N;++i){
for(int j = 0;j<M;++j){
if(grid[i][j] == 'R'){
if(i+2 < N && grid[i+1][j] == 'G' && grid[i+2][j] == 'W'){
++n;
mp[i][j][1] = n;
//cout << i << " " << j << " " << 1 << "\n";
}
if(j+2 < M && grid[i][j+1] == 'G' && grid[i][j+2] == 'W'){
++n;
mp[i][j][0] = n;
//cout << i << " " << j << " " << 0 << "\n";
if(mp[i][j][1]){
adj[n].push_back(mp[i][j][1]);
adj[mp[i][j][1]].push_back(n);
}
if(i-1 >= 0 && mp[i-1][j+1][1]){
adj[n].push_back(mp[i-1][j+1][1]);
adj[mp[i-1][j+1][1]].push_back(n);
}
if(i-2 >= 0 && mp[i-2][j+2][1]){
adj[n].push_back(mp[i-2][j+2][1]);
adj[mp[i-2][j+2][1]].push_back(n);
}
}
}
}
}
vis = vector<bool>(n+1,false);
int ans = 0;
for(int i = 1;i<=n;++i){
if(!vis[i]){
side1 = 0; side2 = 0;
dfs(i,1);
ans += max(side1,side2);
}
}
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... |