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 VERTICAL 1
#define HORIZONTAL 2
#define NOT_DONE 1
#define DONE 2
using namespace std;
void open(){
if(fopen("input.inp", "r")){
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
}
}
const int maxn = 1e5;
int n, m;
char board[20][20];
vector<signed> G[maxn];
signed vertical_change[20][20], horizontal_change[20][20];
pair<signed, signed> vertical_parent[20][20], horizontal_parent[20][20];
signed vertical_state[20][20], horizontal_state[20][20];
signed cnt;
bool check[maxn];
int bfs(int root){
queue<pair<int, bool>> q;
q.push({root, true});
int total = 0;
int cur = 0;
while(!q.empty()){
pair<int, int> first = q.front();
q.pop();
int u = first.first;
if(check[u]) continue;
check[u] = true;
total++;
cur += first.second;
for(int v : G[u]){
q.push({v, first.second ^ 1});
}
}
return max(cur, total - cur);
}
void build(int x, int y, int direction){
if(direction == VERTICAL){
if(vertical_state[x][y]) return;
vertical_state[x][y] = NOT_DONE;
int cur_index = vertical_change[x][y];
int new_index;
if(x + 2 > n || board[x + 1][y] != 'G' || board[x + 2][y] != 'W') return;
// To R:
if(horizontal_parent[x][y] != make_pair(0, 0)){
pair<int, int> temp = make_pair(x, y);
new_index = horizontal_change[x][y];
if(horizontal_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, HORIZONTAL);
}
}
// To G:
if(horizontal_parent[x + 1][y] != make_pair(0, 0)){
pair<int, int> temp = horizontal_parent[x + 1][y];
new_index = horizontal_change[temp.first][temp.second];
if(horizontal_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, HORIZONTAL);
}
}
// To W:
if(horizontal_parent[x + 2][y] != make_pair(0, 0)){
pair<int, int> temp = horizontal_parent[x + 2][y];
new_index = horizontal_change[temp.first][temp.second];
if(horizontal_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, HORIZONTAL);
}
}
vertical_state[x][y] = DONE;
}else{
horizontal_state[x][y] = NOT_DONE;
int cur_index = horizontal_change[x][y];
int new_index;
if(y + 2 > m || board[x][y + 1] != 'G' || board[x][y + 2] != 'W') return;
// To R:
if(vertical_parent[x][y] != make_pair(0, 0)){
pair<int, int> temp = make_pair(x, y);
new_index = vertical_change[x][y];
if(vertical_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, VERTICAL);
}
}
// To G:
if(vertical_parent[x][y + 1] != make_pair(0, 0)){
pair<int, int> temp = vertical_parent[x][y + 1];
new_index = vertical_change[temp.first][temp.second];
if(vertical_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, VERTICAL);
}
}
// To W:
if(vertical_parent[x][y + 1] != make_pair(0, 0)){
pair<int, int> temp = vertical_parent[x][y + 1];
new_index = vertical_change[temp.first][temp.second];
if(vertical_state[temp.first][temp.second] != NOT_DONE){
G[cur_index].push_back(new_index);
G[new_index].push_back(cur_index);
build(temp.first, temp.second, VERTICAL);
}
}
horizontal_state[x][y] = DONE;
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
open();
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> board[i][j];
}
}
cnt = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(board[i][j] == 'R'){
// horizontal:
if(j + 2 <= m && board[i][j + 1] == 'G' && board[i][j + 2] == 'W'){
horizontal_parent[i][j] = make_pair(i, j);
horizontal_change[i][j] = ++cnt;
horizontal_parent[i][j + 1] = horizontal_parent[i][j + 2] = horizontal_parent[i][j];
}
// vertical:
if(i + 2 <= n && board[i + 1][j] == 'G' && board[i + 2][j] == 'W'){
vertical_parent[i][j] = make_pair(i, j);
vertical_change[i][j] = ++cnt;
vertical_parent[i + 1][j] = vertical_parent[i + 2][j] = vertical_parent[i][j];
}
}
}
}
if(cnt == 0){
cout << 0 << endl;
return 0;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(board[i][j] == 'R'){
if(vertical_parent[i][j] != make_pair(0, 0)){
build(i, j, VERTICAL);
}else{
build(i, j, HORIZONTAL);
}
}
}
}
int result = 0;
for(int i = 1; i <= cnt; i++){
if(!check[i]){
result += bfs(i);
}
}
assert(result != 22);
cout << result << endl;
return 0;
}
Compilation message (stderr)
dango_maker.cpp: In function 'void open()':
dango_maker.cpp:12:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
dango_maker.cpp:13:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | freopen("output.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |