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;
using ll = long long;
constexpr int nax = 105;
char mat[nax][nax];
const string str = "RGW";
/*
0 - desine
1 - virsus
*/
bool gali[nax][nax][2];
bool vis[nax][nax][2];
const int dx[] = {-1, +1};
const int dy[] = {+1, -1};
int n, m;
bool in(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
bool check_ok(int i, int j, int p) {
if (i < 0 || j < 0 || i >= n || j >= m) return false;
string curr;
if (p == 0) {
for(int k = 0; k < 3; k++) {
curr += mat[i][j+k];
}
}
else {
for(int k = 0; k < 3; k++) {
curr += mat[i+k][j];
}
}
return (curr == str);
}
int sz = 0;
int cnt = 0;
void dfs(int x, int y, int k, int col) {
if (col == 0) cnt++;
sz++;
vis[x][y][k] = true;
for(int i = 0; i < 2; i++) {
int X = x + dx[i], Y = y + dy[i], K = k^1;
if (in(X, Y) && !vis[X][Y][K] && gali[X][Y][K]) {
dfs(X, Y, K, col^1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if (mat[i][j] == 'G') {
if (j-1 >= 0) {
gali[i][j][0] = check_ok(i, j-1, 0);
}
if (i-1 >= 0) {
gali[i][j][1] = check_ok(i-1, j, 1);
}
}
}
}
// for(int i = 0; i < n; i++) {
// for(int j = 0; j < m; j++) {
// if (mat[i][j] != 'G') continue;
// for(int k = 0; k < 2; k++) {
// if (i-1 >= 0 && j+1 < m && gali[i-1][j+1][k^1]) {
// adj[i][j][k].push_back({i-1, j+1, k^1});
// }
// if (i+1 < n && j-1 >= 0 && gali[i+1][j-1][k^1]) {
// adj[i][j][k].push_back({i+1, j-1, k^1});
// }
// }
// }
// }
int ans = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
for(int k = 0; k < 2; k++) {
if (gali[i][j][k] == false || vis[i][j][k]) continue;
sz = 0;
cnt = 0;
dfs(i, j, k, 0);
ans += max(cnt, sz-cnt);
}
}
}
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... |