QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#136408#3125. Dango MakerSteGG20013 2ms5948kbC++174.9kb2023-08-08 17:42:462023-08-08 17:42:48

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-08 17:42:48]
  • 评测
  • 测评结果:13
  • 用时:2ms
  • 内存:5948kb
  • [2023-08-08 17:42:46]
  • 提交

answer

#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;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 13
Accepted

Test #1:

score: 13
Accepted
time: 2ms
memory: 5816kb

input:

1 1
G

output:

0

result:

ok single line: '0'

Test #2:

score: 0
Accepted
time: 2ms
memory: 5876kb

input:

1 2
RG

output:

0

result:

ok single line: '0'

Test #3:

score: 0
Accepted
time: 2ms
memory: 5804kb

input:

2 1
W
R

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 0ms
memory: 5892kb

input:

3 2
WW
RW
WR

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 5752kb

input:

4 4
GRRW
GWWR
WWWW
RGRG

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 1ms
memory: 5896kb

input:

4 4
RGRR
RRRG
GRGW
RGWW

output:

2

result:

ok single line: '2'

Test #7:

score: 0
Accepted
time: 2ms
memory: 5820kb

input:

4 4
RRGR
GRRG
WRGW
RGWW

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 1ms
memory: 5820kb

input:

4 4
RGWR
GGGW
WWGW
RWGW

output:

1

result:

ok single line: '1'

Test #9:

score: 0
Accepted
time: 2ms
memory: 5948kb

input:

3 3
RGW
GGG
WGW

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 2ms
memory: 5808kb

input:

4 1
W
R
G
W

output:

1

result:

ok single line: '1'

Test #11:

score: 0
Accepted
time: 2ms
memory: 5856kb

input:

4 4
RGWR
GWRG
WRGW
RGWR

output:

3

result:

ok single line: '3'

Test #12:

score: 0
Accepted
time: 1ms
memory: 5832kb

input:

4 4
RWWR
GWRG
WGGW
RGWR

output:

3

result:

ok single line: '3'

Test #13:

score: 0
Accepted
time: 2ms
memory: 5944kb

input:

4 4
RGWR
WWRG
WRGW
RWGR

output:

2

result:

ok single line: '2'

Test #14:

score: 0
Accepted
time: 1ms
memory: 5820kb

input:

4 4
RRRR
GGGG
WWWW
RRRR

output:

4

result:

ok single line: '4'

Test #15:

score: 0
Accepted
time: 1ms
memory: 5816kb

input:

4 4
RRRR
GGGR
WWWW
RRRR

output:

3

result:

ok single line: '3'

Test #16:

score: 0
Accepted
time: 2ms
memory: 5820kb

input:

4 4
RRRR
GGGG
WWWW
RWRR

output:

4

result:

ok single line: '4'

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #17:

score: 20
Accepted
time: 1ms
memory: 5828kb

input:

5 5
RRGRR
RGRGW
RRWRW
RGWGW
RWWWW

output:

3

result:

ok single line: '3'

Test #18:

score: 0
Accepted
time: 1ms
memory: 5852kb

input:

6 6
RGWRGW
RRRGWR
RRWGWR
WRRRWG
GGGGGW
WWWWWW

output:

7

result:

ok single line: '7'

Test #19:

score: 0
Accepted
time: 2ms
memory: 5824kb

input:

7 10
RRRGRGWRGW
RGGGWRRGWR
RWWWWGRRGG
RGWRWWGGGW
WWRGWRGWGW
RGWWGGRGWW
RRGWWWWWWW

output:

14

result:

ok single line: '14'

Test #20:

score: 0
Accepted
time: 0ms
memory: 5884kb

input:

10 8
RGWRRRGW
RGWGRRGW
WRGWGRGW
RGWWRGWW
GWRRGWWW
WRRGRWRR
GRGWGRGG
WGWWWRWR
RGWRGRGW
RRWRGWWW

output:

16

result:

ok single line: '16'

Test #21:

score: 0
Accepted
time: 1ms
memory: 5936kb

input:

10 10
RRRRGWRRGW
GRGRGRGGRR
RGRGWGRRGR
RWWWRRGRGW
GRGGGRGWGG
WRGWWGGRGW
GGGRWWWRRR
WWGRGWRRGG
WWGWGWGGWW
RRGWGRWWWW

output:

16

result:

ok single line: '16'

Test #22:

score: 0
Accepted
time: 2ms
memory: 5816kb

input:

10 10
RRRWRGWRGW
GGGGGGRRWG
WGWRWWGGGW
RRRRRRWRRG
GGGGGGRGGR
WGWWWWGWGW
WRRGWRWRGW
RGWGRGWGRW
GRWRGWWWGG
RGWWGWRGWW

output:

19

result:

ok single line: '19'

Test #23:

score: 0
Accepted
time: 1ms
memory: 5856kb

input:

10 10
WGWGRWWGWR
RGGWGRWWGR
GWRRRRWWWG
RGWRGWRRRG
GRRWWGGRGG
GGGGRWGRGG
RRRGWWWWRW
WRRRWRGRGR
RGWGRWGRWG
WRRWGGGWWW

output:

7

result:

ok single line: '7'

Test #24:

score: 0
Accepted
time: 2ms
memory: 5940kb

input:

10 10
GGRRGRGRWR
RRWRGWWRRW
WGRWWRRRWG
GGWWRWGRGR
RGGGRRGWRR
WRWWWRWWWW
WRWGGGGRRR
RWGRGRWGGW
GWGWWGWGRR
GRWGGGWRWW

output:

2

result:

ok single line: '2'

Test #25:

score: 0
Accepted
time: 2ms
memory: 5808kb

input:

10 1
R
G
R
W
G
G
G
R
G
R

output:

0

result:

ok single line: '0'

Test #26:

score: 0
Accepted
time: 2ms
memory: 5904kb

input:

1 10
GGGRWWGGWW

output:

0

result:

ok single line: '0'

Test #27:

score: 0
Accepted
time: 2ms
memory: 5816kb

input:

10 10
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRRWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR

output:

27

result:

ok single line: '27'

Test #28:

score: 0
Accepted
time: 0ms
memory: 5856kb

input:

10 10
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRWWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRWWRGWRG
RRGWRGWRGW
RGWRGWRGWW

output:

26

result:

ok single line: '26'

Test #29:

score: 0
Accepted
time: 0ms
memory: 5824kb

input:

10 10
RGGRGWGGWR
GGRGWRWWRG
WRGWRGWRGW
GGWGWWRGWR
RWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWWGWRGRRG
WRGWRGGRGW
RGWRGRRGWR

output:

20

result:

ok single line: '20'

Test #30:

score: 0
Accepted
time: 2ms
memory: 5828kb

input:

10 10
RRRRRRRRRR
GGWGGGGGGG
WWWWWWWWWW
RRRRRRRRRR
GGGGGGGGGG
WWWWWWWGWW
RRRRRRRRRR
GGGGGGGGGG
WWWWWWWWWW
RRRRRRRRRR

output:

28

result:

ok single line: '28'

Test #31:

score: 0
Accepted
time: 1ms
memory: 5852kb

input:

10 10
RRGRRRWRRR
GGGGGGGGRG
WWWGWWWWWW
RRRRRRRRRR
GGGGGGGGGG
WGRWWWWWWW
RRRRRRRRRR
GGGGGGGGGG
WWWWWWWWWW
RRRRRRRRRR

output:

24

result:

ok single line: '24'

Test #32:

score: 0
Accepted
time: 2ms
memory: 5820kb

input:

10 10
RGRRRRRGRR
GGGGWGGGGG
GWWWWWWRWW
RRRRWRRRRR
GGGGGGGGGG
WWWWWWWWRW
RRRRRRRRRR
GGGGGWGGGG
GWGWWWWGWW
RWRRRRRRWW

output:

20

result:

ok single line: '20'

Test #33:

score: 0
Accepted
time: 0ms
memory: 5896kb

input:

10 10
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGGRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR

output:

26

result:

ok single line: '26'

Test #34:

score: -20
Wrong Answer
time: 2ms
memory: 5876kb

input:

10 10
RGWRGWRGWR
WWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRGWWGWRG
WRGWRGWRGW
RGWRGWRGWR
GWRGWRGWRG
WRGWRGWRGW
RGWRGWRGWR

output:

26

result:

wrong answer 1st lines differ - expected: '27', found: '26'

Subtask #3:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%