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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <chrono>
#include <cstring>
//#define int long long
#define INP freopen("palpath.in","r",stdin)
#define OUTP freopen("palpath.out","w",stdout)
using namespace std;
int n, m;
const int maxn = 3005;
char arr[maxn][maxn];
int r[maxn][maxn];
int d[maxn][maxn];
bitset<1000000000> used;
int cnt;
vector<vector<int> > v;
int compSize;
int black;
// RGW
void dfs(int u, int color) {
if (color == 0){
black++;
}
used[u] = 1;
for(int i = 0; i < v[u].size();i++){
int to = v[u][i];
if (!used[to]){
dfs(to, !color);
}
}
}
bool check(int x, int y, char dir) {
if (x < 0 || x >= n || y < 0 || y >= m) {
return 0;
}
if (dir == 'r') {
if (y + 2 >= m) {
return 0;
}
if ((arr[x][y] == 'R') && (arr[x][y + 1] == 'G') && (arr[x][y + 2] == 'W')) {
return 1;
}
}
if (dir == 'd') {
if (x + 2 >= n) {
return 0;
}
if ((arr[x][y] == 'R') && (arr[x + 1][y] == 'G') && (arr[x + 2][y] == 'W')) {
return 1;
}
}
return false;
}
int main(){
ios_base::sync_with_stdio(0);
cin >> n >> m;
v.resize(n * m);
for(int i = 0; i < n;i++) {
for(int j = 0; j < m;j++) {
r[i][j] = -1;
d[i][j] = -1;
cin >> arr[i][j];
}
}
int ans = 0;
for(int i = 0; i < n;i++) {
for(int j = 0; j < m; j++) {
if (check(i, j, 'r')) {
r[i][j] = cnt++;
}
if (check(i, j, 'd')) {
d[i][j] = cnt++;
}
}
}
for(int i = 0; i < n;i++) {
for(int j = 0; j < m;j++) {
if (r[i][j] != -1) {
int node = r[i][j];
if (check(i, j, 'd')) {
v[node].push_back(d[i][j]);
}
if (check(i - 1, j + 1, 'd')) {
v[node].push_back(d[i - 1][j + 1]);
}
if (check(i - 2, j + 2, 'd')) {
v[node].push_back(d[i - 2][j + 2]);
}
}
if (d[i][j] != -1) {
int node = d[i][j];
if (check(i, j, 'r')) {
v[node].push_back(r[i][j]);
}
if (check(i + 1, j - 1, 'r')) {
v[node].push_back(r[i + 1][j - 1]);
}
if (check(i + 2, j - 2, 'r')) {
v[node].push_back(r[i + 2][j - 2]);
}
}
}
}
if (cnt == 0) {
cout << 0;
return 0;
}
for(int i = 0; i < cnt; i++) {
compSize = 0;
black = 0;
if (!used[i]){
dfs(i, 0);
}
ans += max(black, compSize - black);
}
cout << ans;
return 0;
}
/*
1
3
1 3 6
*/
Compilation message (stderr)
dango_maker.cpp: In function 'void dfs(int, int)':
dango_maker.cpp:46:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | for(int i = 0; i < v[u].size();i++){
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |