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>
//#define int long long
#define pi pair <int, int>
#define ppi pair <pi, int>
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << '\n'
#define debug2(x, y) cout << #x << ": " << x << ' ' << #y << ": " << y << '\n'
#define debug3(x, y, z) cout << #x << ": " << x << ' ' << #y << ": " << y << ' ' << #z << ": " << z << '\n'
#define debug4(x, y, z, w) cout << #x << ": " << x << ' ' << #y << ": " << y << ' ' << #z << ": " << z << ' ' << #w << ": " << w << '\n'
using namespace std;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const int inf = 1000000000;
int n, m;
string grid[3005], valid[3005];
int ans;
bool visited[3005][3005];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> grid[i], valid[i] = grid[i];
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
if(grid[i][j] == 'R'){
bool check = 0;
if(j+2 < m and grid[i][j+1] == 'G' and grid[i][j+2] == 'W') check = 1;
if(i+2 < n and grid[i+1][j] == 'G' and grid[i+2][j] == 'W') check = 1;
if(!check) valid[i][j] = '.';
}
else if(grid[i][j] == 'G'){
bool check = 0;
if(j-1 >= 0 and j+1 < m and grid[i][j-1] == 'R' and grid[i][j+1] == 'W') check = 1;
if(i-1 >= 0 and i+1 < n and grid[i-1][j] == 'R' and grid[i+1][j] == 'W') check = 1;
if(!check) valid[i][j] = '.';
}
else{
bool check = 0;
if(j-2 >= 0 and grid[i][j-1] == 'G' and grid[i][j-2] == 'R') check = 1;
if(i-2 >= 0 and grid[i-1][j] == 'G' and grid[i-2][j] == 'R') check = 1;
if(!check) valid[i][j] = '.';
}
}
//for(int i = 0; i < n; i++) cout << valid[i] << '\n';
queue <pi> q;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(valid[i][j] == '.' or visited[i][j]) continue;
q.emplace(i, j);
int horizontal = 0, vertical = 0;
while(!q.empty()){
int x = q.front().fi, y = q.front().se;
//debug2(x, y);
q.pop();
visited[x][y] = 1;
if(valid[x][y] == 'R'){
if(y+2 < m and valid[x][y+1] == 'G' and valid[x][y+2] == 'W'){
bool b = 0;
if(!visited[x][y+1]) q.emplace(x, y+1), b = 1;
if(!visited[x][y+2]) q.emplace(x, y+2), b = 1;
visited[x][y+1] = 1, visited[x][y+2] = 1;
if(b) horizontal++;
}
if(x+2 < n and valid[x+1][y] == 'G' and valid[x+2][y] == 'W'){
bool b = 0;
if(!visited[x+1][y]) q.emplace(x+1, y), b = 1;
if(!visited[x+2][y]) q.emplace(x+2, y), b = 1;
visited[x+1][y] = 1, visited[x+2][y] = 1;
if(b) vertical++;
}
}
else if(valid[x][y] == 'G'){
if(y-1 >= 0 and y+1 < m and valid[x][y-1] == 'R' and valid[x][y+1] == 'W'){
bool b = 0;
if(!visited[x][y-1]) q.emplace(x, y-1), b = 1;
if(!visited[x][y+1]) q.emplace(x, y+1), b = 1;
visited[x][y-1] = 1, visited[x][y+1] = 1;
if(b) horizontal++;
}
if(x-1 >= 0 and x+1 < n and valid[x-1][y] == 'R' and valid[x+1][y] == 'W'){
bool b = 0;
if(!visited[x-1][y]) q.emplace(x-1, y), b = 1;
if(!visited[x+1][y]) q.emplace(x+1, y), b = 1;
visited[x-1][y] = 1, visited[x+1][y] = 1;
if(b) vertical++;
}
}
else{
if(y-2 >= 0 and valid[x][y-2] == 'R' and valid[x][y-1] == 'G'){
bool b = 0;
if(!visited[x][y-2]) q.emplace(x, y-2), b = 1;
if(!visited[x][y-1]) q.emplace(x, y-1), b = 1;
visited[x][y-2] = 1, visited[x][y-1] = 1;
if(b) horizontal++;
}
if(x-2 >= 0 and valid[x-2][y] == 'R' and valid[x-1][y] == 'G'){
bool b = 0;
if(!visited[x-2][y]) q.emplace(x-2, y), b = 1;
if(!visited[x-1][y]) q.emplace(x-1, y), b = 1;
visited[x-2][y] = 1, visited[x-1][y] = 1;
if(b) vertical++;
}
}
}
ans += max(horizontal, vertical);
}
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |