Submission #814757

# Submission time Handle Problem Language Result Execution time Memory
814757 2023-08-08T09:39:48 Z SteGG Dango Maker (JOI18_dango_maker) C++17
13 / 100
1 ms 2644 KB
#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;
	assert(n <= 20 && m <= 20);
	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);
		}
	}

	cout << result << endl;

	return 0;
}

Compilation message

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
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 1 ms 2612 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Incorrect 1 ms 2644 KB Output isn't correct
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 1 ms 2644 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2644 KB Output is correct
17 Correct 1 ms 2644 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 1 ms 2644 KB Output is correct
25 Correct 1 ms 2644 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 1 ms 2644 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 1 ms 2644 KB Output is correct
30 Correct 1 ms 2644 KB Output is correct
31 Correct 1 ms 2644 KB Output is correct
32 Correct 1 ms 2612 KB Output is correct
33 Correct 1 ms 2644 KB Output is correct
34 Incorrect 1 ms 2644 KB Output isn't correct
35 Halted 0 ms 0 KB -