[BOJ] 19235 모노미노도미노

Time Lapse :1hour 4min 57sec

19235.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
using namespace std;
int point, n, y, x, b, cur;
int green[6][4], blue[4][6];
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
void doGreen() {
switch(b) {
case 1: {
green[0][x] = cur;
int cy = 0, cx = x;
while(true) {
if(cy+1 >= 6 || green[cy+1][cx]) break;
green[cy][cx] = 0;
green[++cy][cx] = cur;
}
} break;
case 2: {
green[1][x] = green[1][x+1] = cur;
int cy = 1, cx = x;
while(true) {
if(cy + 1 >= 6 || green[cy+1][cx] || green[cy+1][cx+1]) break;
green[cy][cx] = green[cy][cx+1] = false;
++cy;
green[cy][cx] = green[cy][cx+1] = cur;
}
} break;
case 3: {
green[0][x] = green[1][x] = cur;
int cy = 1, cx = x;
while(true) {
if(cy+1 >= 6 || green[cy+1][cx]) break;
green[cy-1][cx] = 0;
green[++cy][cx] = cur;
}
} break;
}
}

void doBlue() {
switch (b) {
case 1: {
blue[y][0] = cur;
int cy = y, cx = 0;
while(true) {
if(cx + 1>= 6 || blue[cy][cx+1]) break;
blue[cy][cx] = 0;
blue[cy][++cx] = cur;
}
} break;
case 2: {
blue[y][0] = blue[y][1] = cur;
int cy = y, cx = 1;
while(true) {
if(cx + 1 >= 6 || blue[cy][cx+1]) break;
blue[cy][cx-1] = 0;
blue[cy][cx + 1] = cur;
++cx;
}
} break;
case 3: {
blue[y][1] = blue[y+1][1] = cur;
int cy = y, cx = 1;
while(true) {
if(cx + 1 >= 6 || blue[cy][cx+1] || blue[cy+1][cx+1]) break;
blue[cy][cx] = blue[cy+1][cx] = 0;
++cx;
blue[cy][cx] = blue[cy+1][cx] = cur;
}
} break;
}
}

void moveGreen() {
for(int i = 4; i >= 0; --i) {
for(int j = 0; j < 4; ++j) {
if(green[i][j]) {
int cx1 = j, cy1 = i, cy2 = i, cx2 = j;
for(int k = 0; k < 4; ++k) {
if(0 <= i + dy[k] && i + dy[k] < 6 && 0 <= j + dx[k] && j + dx[k] < 4 && green[i+dy[k]][j+dx[k]] == green[i][j])
cy2 = i + dy[k], cx2 = j + dx[k];
}
while(true) {
if(cy1 + 1 >= 6 || cy2 + 1 >= 6) break;
if(green[cy1+1][cx1] != 0 && green[cy1+1][cx1] != green[cy1][cx1]) break;
if(green[cy2+1][cx2] != 0 && green[cy2+1][cx2] != green[cy2][cx2]) break;
green[cy1+1][cx1] = green[cy1][cx1];
green[cy2+1][cx2] = green[cy2][cx2];
green[cy2][cx2] = 0;
if(cy2 + 1 != cy1)
green[cy1][cx1] = 0;
++cy1;
++cy2;
}
}
}
}
}

void moveBlue() {
for(int i = 4; i >= 0; --i) {
for(int j = 0; j < 4; ++j) {
if(blue[j][i]) {
int cx1 = i, cy1 = j, cx2 = i, cy2 = j;
for(int k = 0; k < 4; ++k) {
if(0 <= j + dy[k] && j + dy[k] < 4 && 0 <= i + dx[k] && i + dx[k] < 6 && blue[j + dy[k]][i+dx[k]] == blue[j][i])
cx2 = i+dx[k], cy2 = j + dy[k];
}
while(true) {
if(cx1 + 1 >= 6 || cx2 + 1 >= 6) break;
if(blue[cy1][cx1+1] != 0 && blue[cy1][cx1+1] != blue[cy1][cx1]) break;
if(blue[cy2][cx2+1] != 0 && blue[cy2][cx2+1] != blue[cy2][cx2]) break;
blue[cy1][cx1+1] = blue[cy1][cx1];
blue[cy2][cx2+1] = blue[cy2][cx2];
blue[cy2][cx2] = 0;
if(cx2+1 != cx1)
blue[cy1][cx1] = 0;
++cx1;
++cx2;
}
}
}
}
}

bool addGreen() {
bool ret = false;
for(int i = 2; i < 6; i++) {
int cnt = 0;
for(int j = 0; j < 4; ++j)
if(green[i][j]) ++cnt;
if(cnt == 4) {
for (int j = 0; j < 4; ++j)
green[i][j] = 0;
++point;
ret = true;
}
}
moveGreen();
return ret;
}
void popGreen() {
while(addGreen());
while(green[0][0] || green[1][0] || green[0][1] || green[1][1] ||green[0][2] || green[1][2] ||green[0][3] || green[1][3]) {
green[5][0] = green[5][1] = green[5][2] = green[5][3] = 0;
moveGreen();
while(addGreen());
}
}
bool addblue() {
bool ret = false;
for(int i = 2; i < 6; i++) {
int cnt = 0;
for(int j = 0; j < 4; ++j)
if(blue[j][i]) ++cnt;
if(cnt == 4) {
for(int j = 0; j < 4; ++j)
blue[j][i] = 0;
++point;
ret = true;
}
}
moveBlue();
return ret;
}
void popBlue() {
while(addblue());
while(blue[0][0] || blue[0][1] || blue[1][0] || blue[1][1] ||blue[2][0] || blue[2][1] ||blue[3][0] || blue[3][1]) {
blue[0][5] = blue[1][5] = blue[2][5] = blue[3][5] = 0;
moveBlue();
while(addblue());
}
}
int blocks() {
int ret = 0;
for(int i = 0; i < 6; i++)
for(int j = 0; j < 4; ++j) {
if(green[i][j]) ++ret;
if(blue[j][i]) ++ret;
}
return ret;
}
int main() {
scanf("%d",&n);
for(cur = 1; cur <= n; ++cur) {
scanf("%d %d %d",&b,&y,&x);
doGreen();
doBlue();
popGreen();
popBlue();
}
printf("%d\n%d",point,blocks());
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/09/01/PS/BOJ/19235/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.