12100 2048 (Easy)


*핵심 아이디어 : 

1.지역변수로 tmp배열을 두어 매순간마다 복사하며 사용하였다.


2.block_move 함수의 구현.



*문제풀이


1. 상하좌우 4방향에 대해 움직였을때 처리해줄 block_move 함수 작성.

--> block_move 함수의 주석 참고.


2. 모든경우에 대해 완전탐색.


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
#include <iostream>
#include <algorithm>
#define P pair<int,int>
#define SZ 20
using namespace std;
 
int n, res;
int map[SZ][SZ];
 
void block_move(int map[][SZ], int dir) {
    //1상,2하,3좌,4우
    if (dir == 1) {
        for (int i = 0; i < n; i++) {
            int ax = 0;
            int bx = ax + 1;
            while (bx <= n - 1) {
                if (map[bx][i] != 0) {
                    //ax가 비어있는경우
                    if (map[ax][i] == 0) {
                        map[ax][i] = map[bx][i];
                        map[bx][i] = 0;
                    }
                    //ax가 비어있지 않은 경우
                    else {
                        //ax와 bx가 같은 경우
                        if (map[ax][i] == map[bx][i]) {
                            map[ax][i] = (map[ax][i] << 1);
                            map[bx][i] = 0;
                            ax++;
                        }
                        //ab와 bx가 다른경우
                        else {
                            //한칸차이
                            if (bx - ax == 1) {
                                ax++;
                            }
                            //두칸 이상 차이
                            else {
                                ax++;
                                map[ax][i] = map[bx][i];
                                map[bx][i] = 0;
                            }
                        }
                    }
                }
                bx++;
            }
        }
    }
    //하
    else if (dir == 2) {
        for (int i = 0; i < n; i++) {
            int ax = n - 1;
            int bx = ax - 1;
            while (bx >= 0) {
                if (map[bx][i] != 0) {
                    if (map[ax][i] == 0) {
                        map[ax][i] = map[bx][i];
                        map[bx][i] = 0;
                    }
                    else {
                        if (map[ax][i] == map[bx][i]) {
                            map[ax][i] = (map[ax][i] << 1);
                            map[bx][i] = 0;
                            ax--;
                        }
                        else {
                            if (ax - bx == 1) {
                                ax--;
                            }
                            else {
                                ax--;
                                map[ax][i] = map[bx][i];
                                map[bx][i] = 0;
                            }
                        }
                    }
                }
                bx--;
            }
        }
    }
    //좌
    else if (dir == 3) {
        for (int i = 0; i < n; i++) {
            int ax = 0;
            int bx = ax + 1;
            while (bx <= n - 1) {
                if (map[i][bx] != 0) {
                    if (map[i][ax] == 0) {
                        map[i][ax] = map[i][bx];
                        map[i][bx] = 0;
                    }
                    else {
                        if (map[i][ax] == map[i][bx]) {
                            map[i][ax] = (map[i][ax] << 1);
                            map[i][bx] = 0;
                            ax++;
                        }
                        else {
                            if (bx - ax == 1) {
                                ax++;
                            }
                            else {
                                ax++;
                                map[i][ax] = map[i][bx];
                                map[i][bx] = 0;
                            }
                        }
                    }
                }
                bx++;
            }
        }
    }
    //우
    else {
        for (int i = 0; i < n; i++) {
            int ax = n - 1;
            int bx = ax - 1;
            while (bx >= 0) {
                if (map[i][bx] != 0) {
                    if (map[i][ax] == 0) {
                        map[i][ax] = map[i][bx];
                        map[i][bx] = 0;
                    }
                    else {
                        if (map[i][ax] == map[i][bx]) {
                            map[i][ax] = (map[i][ax] << 1);
                            map[i][bx] = 0;
                            ax--;
                        }
                        else {
                            if (ax - bx == 1) {
                                ax--;
                            }
                            else {
                                ax--;
                                map[i][ax] = map[i][bx];
                                map[i][bx] = 0;
                            }
                        }
                    }
                }
                bx--;
            }
        }
    }
}
 
//디버깅용
void printAll(int map[][SZ]) {
    cout << endl;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
}
 
int getResult(int tmp[][SZ]) {
    int ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ans = max(ans, tmp[i][j]);
        }
    }
    return ans;
}
 
void dfs(int map[][SZ], int dir, int cnt) {
 
    if (cnt == 6) {
        res = max(res, getResult(map));
    }
    else {
        int tmp[SZ][SZ];
        //배열 복사
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                tmp[i][j] = map[i][j];
            }
        }
 
        block_move(tmp, dir);
        for (int i = 1; i < 5; i++) {
            dfs(tmp, i, cnt + 1);
        }
    }
}
 
int main() {
    ios_base::sync_with_stdio(false);
 
    cin >> n;
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
        }
    }
 
    for (int i = 1; i < 5; i++) {
        dfs(map, i, 1);
    }
 
    cout << res << endl;
 
    return 0;
}
cs


'BOJ::문제풀이' 카테고리의 다른 글

14620 꽃길  (0) 2018.04.05
1963 소수경로  (0) 2018.04.02
1799 비숍  (0) 2018.03.29
13549 숨바꼭질 3  (0) 2018.03.26
12851 숨바꼭질 2  (0) 2018.03.26

+ Recent posts