본문 바로가기

시뮬레이션과 구현

모노미노도미노 2

https://www.acmicpc.net/problem/20061

 

20061번: 모노미노도미노 2

모노미노도미노는 아래와 같이 생긴 보드에서 진행되는 게임이다. 보드는 빨간색 보드, 파란색 보드, 초록색 보드가 그림과 같이 붙어있는 형태이다. 게임에서 사용하는 좌표 (x, y)에서 x는 행,

www.acmicpc.net

구현밖에 없다.

간결하게 구현해야 한다.

지저분하면 디버깅도 어렵고 코드만 길어진다.

 

맵을 10 x 10을 설정할 필요는 없어보인다.

int green_map[6][6];
int blue_map[6][6];

을 설정하자.

 

고정되어 있는 행과 열이 존재하므로

그걸 잘 이용해서

while문을 이용해 숫자로 배치를 하자.

green_fall()과

blue_fall()을 만들어서 내리면 된다.

 

1. green_full(), blue_full()

가득 찬 행 / 열이 사라질 때!

주의해야 할 것이 있다.

 

행이나 열이 타일로 가득찬 경우와

연한 칸에 블록이 있는 경우가 동시에 발생할 수 있다.

이 경우에는 행이나 열이 타일로 가득 찬 경우가 없을 때까지

점수를 획득하는 과정이 모두 진행된 후,

연한 칸에 블록이 있는 경우를 처리해야 한다.

 

 

1. 해당 행 / 열을 0으로 초기화

2. 지워진 행 / 열 부터 처음까지(연한 칸 포함) 한 줄씩 땡겨준다.

3. 제일 위에를 0으로 초기화

 

내려온 다음에 다시 가득찬 경우 생길 수 있으므로

"해당 인덱스"부터 다시 full 인지 체크를 해봐야한다.  

 

마지막에 i++, j++을 넣어서

i--, j--를 만나서 해 당 줄을 다시 체크하게 된다.

 

3. green_delete(), blue_delete()

연한 칸에 값이 존재한다면, cnt를 증가시켜서

그 만큼 행/ 열을 땡겨준다.

5 - cnt 부터 0까지

i + cnt / j + cnt 값을 바꿔준다.

 

그리고 연한 칸을 0으로 초기화한다.

 

<소스 코드>
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
// 빨강 블럭은 의미가 없다
int green_map[6][6];
int blue_map[6][6];
int ans = 0;
 
// 행
void green_fall(int t, int x, int y)
{
    if (t == 1)
    {
        int h = 0;
        while (h <= 5 && green_map[h][y] == 0)
            h++;
 
        h--;
        green_map[h][y] = t;
    }
    else if (t == 2)
    {
        int h = 0;
        while (h <= 5 && ((green_map[h][y] == 0&& (green_map[h][y + 1== 0)))
            h++;
        h--;
        green_map[h][y] = t;
        green_map[h][y + 1= t;
    }
    else if (t == 3)
    {
        int h = 0;
        while (h <= 4 && ((green_map[h][y] == 0&& (green_map[h + 1][y] == 0)))
            h++;
 
        h--;
        green_map[h][y] = t;
        green_map[h + 1][y] = t;
    }
}
 
void green_full()
{
    // 연한 색은 안건들인다!
    for (int i = 5; i >= 2; i--)
    {
        bool flag = true;
        for (int j = 0; j <= 3; j++)
        {
            if (green_map[i][j] == 0)
            {
                flag = false;
                break;
            }
        }
 
        if (flag)
        {
            ans++;
            for (int j = 0; j <= 3; j++)
            {
                green_map[i][j] = 0;
            }
            for (int j = (i - 1); j >= 0; j--)
            {
                for (int k = 0; k <= 3; k++)
                    green_map[j + 1][k] = green_map[j][k];
            }
            // 내려가면 제일 위에는 0
            for (int j = 0; j <= 3; j++)
            {
                green_map[0][j] = 0;
            }
 
            // 내려오고 다시 확인해야하니까
            i++;
            // 그리고 바로 i--를 만나게되서 i는 유지됨
        }
    }
}
 
void green_delete()
{
    int cnt = 0;
    for (int i = 0; i <= 1; i++)
    {
        for (int j = 0; j <= 3; j++)
        {
            if (green_map[i][j] > 0)
            {
                cnt++;
                break;
            }
        }
    }
 
    for (int i = (5 - cnt); i >= 0; i--)
    {
        for (int j = 0; j <= 3; j++)
        {
            green_map[i + cnt][j] = green_map[i][j];
        }
    }
 
    for (int i = 0; i <= 1; i++)
    {
        for (int j = 0; j <= 3; j++)
        {
            green_map[i][j] = 0;
        }
    }
}
 
// 열
void blue_fall(int t, int x, int y)
{
    if (t == 1)
    {
        int h = 0;
        while (h <= 5 && blue_map[x][h] == 0)
            h++;
 
        h--;
        blue_map[x][h] = t;
    }
    else if (t == 2)
    {
        int h = 0;
        while (h <= 4 && ((blue_map[x][h] == 0&& (blue_map[x][h + 1== 0)))
            h++;
 
        h--;
        blue_map[x][h] = t;
        blue_map[x][h + 1= t;
 
 
    }
    else if (t == 3)
    {
        int h = 0;
        while (h <= 5 && ((blue_map[x][h] == 0&& (blue_map[x + 1][h] == 0)))
            h++;
        h--;
        blue_map[x][h] = t;
        blue_map[x + 1][h] = t;
    }
}
 
void blue_full()
{
    // 연한 색은 안건들인다!
    for (int j = 5; j >= 2; j--)
    {
        bool flag = true;
        for (int i = 0; i <= 3; i++)
        {
            if (blue_map[i][j] == 0)
            {
                flag = false;
                break;
            }
        }
 
        if (flag)
        {
            ans++;
            for (int i = 0; i <= 3; i++)
            {
                blue_map[i][j] = 0;
            }
            for (int i = (j - 1); i >= 0; i--)
            {
                for (int k = 0; k <= 3; k++)
                    blue_map[k][i + 1= blue_map[k][i];
            }
            // 오른쪽 가면 제일 왼쪽에는 0
            for (int i = 0; i <= 3; i++)
            {
                blue_map[i][0= 0;
            }
 
            // 오른쪽 가고 다시 확인해야하니까
            j++;
            // 그리고 바로 j--를 만나게되서 j는 유지됨
        }
    }
}
 
void blue_delete()
{
    int cnt = 0;
    for (int j = 0; j <= 1; j++)
    {
        for (int i = 0; i <= 3; i++)
        {
            if (blue_map[i][j] > 0)
            {
                cnt++;
                break;
            }
        }
    }
 
    for (int j = (5 - cnt); j >= 0; j--)
    {
        for (int i = 0; i <= 3; i++)
        {
            blue_map[i][j + cnt] = blue_map[i][j];
        }
    }
 
    for (int j = 0; j <= 1; j++)
    {
        for (int i = 0; i <= 3; i++)
        {
            blue_map[i][j] = 0;
        }
    }
}
 
int green_size()
{
    int tmp = 0;
    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 3; j++)
        {
            if (green_map[i][j] != 0)
                tmp++;
        }
    }
    return tmp;
}
 
int blue_size()
{
    int tmp = 0;
    for (int i = 0; i <= 3; i++)
    {
        for (int j = 0; j <= 5; j++)
        {
            if (blue_map[i][j] != 0)
                tmp++;
        }
    }
    return tmp;
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    int n;
    cin >> n;
 
    for (int i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
 
        green_fall(a, b, c);
        blue_fall(a, b, c);
        green_full();
        green_delete();
        blue_full();
        blue_delete();
    }
 
    cout << ans << '\n';
    cout << green_size() + blue_size() << '\n';
 
    return 0;
}
 
cs

'시뮬레이션과 구현' 카테고리의 다른 글

마법사 상어와 블리자드  (0) 2023.10.11
스타트 택시  (1) 2023.10.10
새로운 게임 2  (0) 2023.10.09
원판 돌리기  (1) 2023.10.08
상어 중학교  (0) 2023.10.07