본문 바로가기

브루트 포스/브루트포스(비트마스크)

2048 (Easy)

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

 

12100번: 2048 (Easy)

첫째 줄에 보드의 크기 N (1 ≤ N ≤ 20)이 주어진다. 둘째 줄부터 N개의 줄에는 게임판의 초기 상태가 주어진다. 0은 빈 칸을 나타내며, 이외의 값은 모두 블록을 나타낸다. 블록에 쓰여 있는 수는 2

www.acmicpc.net

 

비트마스크는 언제 쓸 수 있는가?

map 전체를 움직이고,

전체 경우의 수를 계산할 수 있을 때!

위, 아래, 오른쪽, 왼쪽으로 4가지 경우에

최대 5번 이동 하니까 4 ^ 5

 

4 ^ 5 = 2 ^ 10

for (int k = 0; k < (1 << 10); k++) -> 전체 경우의 수

하고 4진수로 나타내면 되겠다

a[i] = (k & 3);
k >>= 2;

 

 

이 조건들 때문에 시간을 너무 많이 썼다 ㅜ

1. 한 번의 이동에서 이미 합쳐진 블록은 또 다른 블록과 다시 합쳐질 수 없다. 

합쳐졌는지를 확인해야 할 bool 배열이 필요하다.

 

2. 똑같은 수가 세 개가 있는 경우에는 이동하려고 하는 쪽의 칸이 먼저 합쳐진다.

아래로 가면 for 문을 n -1 부터 돌린다.위로 가면 for문을 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
#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
// 0 ,1 -> 오, 왼 
// 2 ,3 -> 아래, 위
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
 
int n;
int LIMIT = 10;
 
vector<int> gen(int k)
{
    vector<int> a(5);
    for (int i = 0; i < 5; i++)
    {
        a[i] = (k & 3);
        k >>= 2;
        // a[i] = k % 4;
        // k /= 4;
    }
    return a;
}
 
int simulate(vector<vector<int>> map, vector<int>& dir)
{
    for (int k : dir)
    {
        vector<vector<bool>> merged(n, vector<bool>(n, false));
 
        // 오른쪽
        if (k == 0)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = n - 1; j >= 0; j--)
                {
                    if (map[i][j] != 0)
                    {
                        int x = i;
                        int y = j;
                        if (merged[x][y])
                            continue;
                        while (true)
                        {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                            {
                                break;
                            }
 
                            if (map[nx][ny] == 0)
                            {
                                map[nx][ny] = map[x][y];
                                map[x][y] = 0;
                                x = nx;
                                y = ny;
                            }
                            else if (map[nx][ny] == map[x][y] && !merged[nx][ny])
                            {
                                map[nx][ny] += map[x][y];
                                map[x][y] = 0;
                                merged[nx][ny] = true;
                                break;
                            }
                            else if (map[nx][ny] == map[x][y] && merged[nx][ny])
                            {
                                break;
                            }
                            else if (map[nx][ny] != map[x][y])
                            {
                                break;
                            }
 
                        }
                    }
                }
            }
        }
        else if (k == 1//왼쪽
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (map[i][j] != 0)
                    {
                        int x = i;
                        int y = j;
                        if (merged[x][y])
                            continue;
                        while (true)
                        {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                            {
                                break;
                            }
 
                            if (map[nx][ny] == 0)
                            {
                                map[nx][ny] = map[x][y];
                                map[x][y] = 0;
                                x = nx;
                                y = ny;
                            }
                            else if (map[nx][ny] == map[x][y] && !merged[nx][ny])
                            {
                                map[nx][ny] += map[x][y];
                                map[x][y] = 0;
                                merged[nx][ny] = true;
                                break;
                            }
                            else if (map[nx][ny] == map[x][y] && merged[nx][ny])
                            {
                                break;
                            }
                            else if (map[nx][ny] != map[x][y])
                            {
                                break;
                            }
 
                        }
                    }
                }
            }
        }
        else if (k == 2// 아래
        {
            for (int j = 0; j < n; j++)
            {
                for (int i = n - 1; i >= 0; i--)
                {
                    if (map[i][j] != 0)
                    {
                        int x = i;
                        int y = j;
                        if (merged[x][y])
                            continue;
                        while (true)
                        {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                            {
                                break;
                            }
 
                            if (map[nx][ny] == 0)
                            {
                                map[nx][ny] = map[x][y];
                                map[x][y] = 0;
                                x = nx;
                                y = ny;
                            }
                            else if (map[nx][ny] == map[x][y] && !merged[nx][ny])
                            {
                                map[nx][ny] += map[x][y];
                                map[x][y] = 0;
                                merged[nx][ny] = true;
                                break;
                            }
                            else if (map[nx][ny] == map[x][y] && merged[nx][ny])
                            {
                                break;
                            }
                            else if (map[nx][ny] != map[x][y])
                            {
                                break;
                            }
                        }
                    }
                }
 
            }
        }
        else if (k == 3// 위
        {
            for (int j = 0; j < n; j++)
            {
                for (int i = 0; i < n; i++)
                {
                    if (map[i][j] != 0)
                    {
                        int x = i;
                        int y = j;
                        if (merged[x][y])
                            continue;
                        while (true)
                        {
                            int nx = x + dx[k];
                            int ny = y + dy[k];
                            if (nx < 0 || nx >= n || ny < 0 || ny >= n)
                            {
                                break;
                            }
 
                            if (map[nx][ny] == 0)
                            {
                                map[nx][ny] = map[x][y];
                                map[x][y] = 0;
                                x = nx;
                                y = ny;
                            }
                            else if (map[nx][ny] == map[x][y] && !merged[nx][ny])
                            {
                                map[nx][ny] += map[x][y];
                                map[x][y] = 0;
                                merged[nx][ny] = true;
                                break;
                            }
                            else if (map[nx][ny] == map[x][y] && merged[nx][ny])
                            {
                                break;
                            }
                            else if (map[nx][ny] != map[x][y])
                            {
                                break;
                            }
                        }
                    }
                }
 
            }
 
        }
    }
 
    int tmp = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (map[i][j] > tmp)
                tmp = map[i][j];
        }
    }
    return tmp;
}
 
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> n;
 
    vector<vector<int>> map(n + 5vector<int>(n + 5));
 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
            cin >> map[i][j];
    }
 
    int ans = 0;
    
 
    // 2 ^ 10 = 4 ^ 5
 
    for (int k = 0; k < (1 << LIMIT); k++)
    {
        // 4진법으로 표현된 방향이 들어간다.
        vector<int> dir = gen(k);
        int cur = simulate(map, dir);
 
        if (cur > ans)
            ans = cur;
    }
    cout << ans << '\n';
    return 0;
}
cs

'브루트 포스 > 브루트포스(비트마스크)' 카테고리의 다른 글

구슬 탈출 2 (중요!!)  (0) 2023.09.21
가르침 (어렵다..)  (0) 2023.09.16
종이 조각 (중요!!!)  (0) 2023.03.03
스타트와 링크 with 비트마스크  (0) 2023.03.01
부분 집합의 합  (0) 2023.03.01