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];
vector<array<int, 3>> adj[nax][nax][2];
bool vis[nax][nax][2];
int n, 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;
void dfs(int x, int y, int k) {
sz++;
vis[x][y][k] = true;
for(const auto& u : adj[x][y][k]) {
if (vis[u[0]][u[1]][u[2]]) continue;
dfs(u[0], u[1], u[2]);
}
}
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;
dfs(i, j, k);
ans += (sz+1) / 2;
}
}
}
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... |