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 IO ios::sync_with_stdio(0), cin.tie(0)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
using namespace std;
using pii = pair<int, int>;
void dbg() {;}
template<class T, class ...U>
void dbg(T a, U ...b) { cout << a << " "; dbg(b...); }
void ent() { cout << "\n"; }
const int INF = 1e17;
const int N = 105;
char a[N][N];
int vis[N][N];
int n, m;
// vertical lines / horizontal line
int ver = 0, hor = 0;
void dfs(int x, int y) {
vis[x][y] = true;
if (a[x][y] == 'R') {
if (x + 2 <= n && a[x + 1][y] == 'G' && a[x + 2][y] == 'W')
ver += 1;
if (y + 2 <= m && a[x][y + 1] == 'G' && a[x][y + 2] == 'W')
hor += 1;
}
if (a[x][y] == 'G') {
if (x - 1 >= 1 && x + 1 <= n && a[x - 1][y] == 'R' && a[x + 1][y] == 'W') {
if (!vis[x - 1][y])
dfs(x - 1, y);
if (!vis[x + 1][y])
dfs(x + 1, y);
}
if (y - 1 >= 1 && y + 1 <= m && a[x][y - 1] == 'R' && a[x][y + 1] == 'W') {
if (!vis[x][y - 1])
dfs(x, y - 1);
if (!vis[x][y + 1])
dfs(x, y + 1);
}
}
if (a[x][y] == 'W') {
if (x - 2 >= 1 && a[x - 2][y] == 'R' && a[x - 1][y] == 'W') {
if (!vis[x - 2][y])
dfs(x - 2, y);
if (!vis[x - 1][y])
dfs(x - 1, y);
}
if (y - 2 >= 1 && a[x][y - 2] == 'R' && a[x][y - 1] == 'W') {
if (!vis[x][y - 2])
dfs(x, y - 2);
if (!vis[x][y - 1])
dfs(x, y - 1);
}
}
}
signed main() {
IO;
cin >> n >> m;
FOR(i, 1, n)
FOR(j, 1, m)
cin >> a[i][j];
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!vis[i][j]) {
ver = hor = 0;
dfs(i, j);
ans += max(ver, hor);
}
}
}
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... |