처음에 풀었을 때는 0이 아닌부분을 다 밀어서 합쳤었는데..

스터디 중에 친구가 0이 아닌 부분은 다 큐에넣어서 처리하면 편하다고 해서 그렇게 해보았는데 

굉장히쉽더라구요???

0이아니면 방향대로 큐에 넣어서 큐에서 순서대로 꺼내며 같으면 합치는 방법으로 구현했습니다.

어려운 문제는 아닌 것 같습니다!!(처음에 풀 땐 굉장히 어려웠지만요..ㅠㅠ)

void merge()가 핵심 함수 인듯 싶습니다.



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
#include <stdio.h>
#include <iostream>
#include <queue>
 
#define MAX_SIZE 25
 
using namespace std;
 
int n;
int map[MAX_SIZE][MAX_SIZE];
int ret;
void merge(int d)
{
    queue<int> q;
    int cnt = 0;
 
 
    switch (d){
 
    case 0://위
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (map[j][i] != 0) q.push(map[j][i]);
                map[j][i] = 0;
            }
 
            int idx = 0;
            int pop_data;
 
            while (!q.empty()){
                pop_data = q.front();
                q.pop();
 
                if (map[idx][i] == 0) map[idx][i] = pop_data;
                else if (map[idx][i] == pop_data){
                    map[idx][i] *= 2;
                    idx++;
                }
                else map[++idx][i] = pop_data;
            }
 
        }
        break;
    case 1://아래
        for (int i = 0; i < n; i++){
            for (int j = n - 1; j >= 0; j--){
                if (map[j][i] != 0) q.push(map[j][i]);
                map[j][i] = 0;
            }
 
            int idx = n - 1;
            int pop_data;
 
            while (!q.empty()){
                pop_data = q.front();
                q.pop();
 
                if (map[idx][i] == 0) map[idx][i] = pop_data;
                else if (map[idx][i] == pop_data){
                    map[idx][i] *= 2;
                    idx--;
                }
                else map[--idx][i] = pop_data;
            }
        }
        break;
    case 2://왼
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (map[i][j] != 0) q.push(map[i][j]);
                map[i][j] = 0;
            }
 
            int idx = 0;
            int pop_data;
 
            while (!q.empty()){
                pop_data = q.front();
                q.pop();
 
                if (map[i][idx] == 0) map[i][idx] = pop_data;
                else if (map[i][idx] == pop_data){
                    map[i][idx] *= 2;
                    idx++;
                }
                else map[i][++idx] = pop_data;
            }
 
        }
        break;
 
    case 3://오
        for (int i = 0; i < n; i++){
            for (int j = n - 1; j >= 0; j--){
                if (map[i][j] != 0) q.push(map[i][j]);
                map[i][j] = 0;
            }
 
            int idx = n - 1;
            int pop_data;
 
            while (!q.empty()){
                pop_data = q.front();
                q.pop();
 
                if (map[i][idx] == 0) map[i][idx] = pop_data;
                else if (map[i][idx] == pop_data){
                    map[i][idx] *= 2;
                    idx--;
                }
                else map[i][--idx] = pop_data;
            }
        }
        break;
 
 
    }
}
 
void copy(int(*arr)[MAX_SIZE], int(*arr2)[MAX_SIZE]){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            arr[i][j] = arr2[i][j];
        }
    }
}
 
void dfs(int d)
{
    if (d == 5){
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (map[i][j] > ret) ret = map[i][j];
            }
 
        }
        return;
    }
 
    int copymap[MAX_SIZE][MAX_SIZE];
    copy(copymap, map);
 
    for (int i = 0; i < 4; i++){
        merge(i);
        dfs(d + 1);
        copy(map, copymap);
    }
}
 
 
void input(){
    scanf("%d"&n);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            scanf("%d"&map[i][j]);
        }
    }
}
 
void print(){
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++)
            printf("%d ", map[i][j]);
 
        puts("");
    }
}
 
int main(){
    input();
    dfs(0);
    printf("%d\n", ret);
    return 0;
}
cs


'알고리즘 > 탐색' 카테고리의 다른 글

[5427] 불  (0) 2017.04.16
[13460] 째로탈출2  (2) 2017.04.16
[5214] 환승  (0) 2017.04.04
[9009] 피보나치  (0) 2017.04.04
[9328] 열쇠  (0) 2017.04.02

+ Recent posts