#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3000 + 24;
int n, m; char a[N][N]; int id[N][N];
vector<vector<int>> g;
int tdfs = 0; vector<int> mr, vis; vector<bool> ml;
bool ok(int i, int j, int dir) {
if (dir == 0) {
return (j + 2 <= m && a[i][j] == 'R' && a[i][j + 1] == 'G' && a[i][j + 2] == 'W');
}
else {
return (i + 2 <= n && a[i][j] == 'R' && a[i + 1][j] == 'G' && a[i + 2][j] == 'W');
}
}
bool kuhn(int u) {
if (vis[u] == tdfs) return 0;
vis[u] = tdfs;
for (int v: g[u]) {
if (mr[v] == 0 || kuhn(mr[v])) {
mr[v] = u;
return 1;
}
}
return 0;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
int szL = 0, szT = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (ok(i, j, 0)) ++szL;
if (ok(i, j, 1)) id[i][j] = ++szT;
}
}
g.assign(szL + 1, vector<int>());
mr.resize(szT + 1, 0); vis.resize(szL + 1, 0);
ml.resize(szL + 1, 0);
szL = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (!ok(i, j, 0)) continue;
szL++;
if (ok(i, j, 1)) g[szL].push_back(id[i][j]);
if (i - 1 >= 1 && j + 1 <= m && ok(i - 1, j + 1, 1)) g[szL].push_back(id[i - 1][j + 1]);
if (i - 2 >= 1 && j + 2 <= m && ok(i - 2, j + 2, 1)) g[szL].push_back(id[i - 2][j + 2]);
}
}
int cnt_match = 0;
for (bool run = true; run;) {
run = false; ++tdfs;
for (int i = 1; i <= szL; i++) {
if (!ml[i] && kuhn(i)) {
run = true;
cnt_match++;
ml[i] = true;
}
}
}
cout << (szL + szT) - cnt_match << '\n';
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int T = 1; // cin >> T;
while (T--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |