Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

charminseok

[백준] 12100. 2048 (easy) 본문

알고리즘/알고리즘 문제

[백준] 12100. 2048 (easy)

charminseok 2019. 9. 18. 17:06

블록을 4방향으로 움직이는데 최대 5번 움직였을때 최댓값을 구하는 문제였다. 

DFS로 몇번 반복할 것인지 설정하고, 방향에 따라 숫자를 합해줬다.

처음에 숫자를 합칠때, 합쳐진것에서 또 합쳐질 수 있도록해서 틀렸는데, 고치니까 바로 맞았다....

2 2 4 2가 왼쪽으로 합쳐진다고하면

4 4 2 가 되야되는데

8 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
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
using namespace std;
 
int n;
vector<vector<int>> map;
 
void func(int d) {
    vector<int> t;
    if (d == 0) {//왼
        for (int i = 0; i < n; i++) {
            t.clear();
            for (int j = 0; j < n; j++) {
                if (map[i][j] != 0)
                    t.push_back(map[i][j]);
                map[i][j] = 0;
            }
            if (t.size() == 0)
                continue;
            for (int j = 0; j < t.size() - 1; j++) {
                if (t[j] == t[j + 1]) {
                    t[j] *= 2;
                    t.erase(t.begin() + (j + 1));
                }
            }
            for (int j = 0; j < t.size(); j++) {
                map[i][j] = t[j];
            }
        }
    }
    else if (d == 1) {//오
        for (int i = 0; i < n; i++) {
            t.clear();
 
            for (int j = n - 1; j >= 0; j--) {
                if (map[i][j] != 0)
                    t.push_back(map[i][j]);
                map[i][j] = 0;
            }
            if (t.size() == 0)
                continue;
            for (int j = 0; j < t.size() - 1; j++) {
                if (t[j] == t[j + 1]) {
                    t[j] *= 2;
                    t.erase(t.begin() + (j + 1));
                }
            }
            for (int j = 0; j < t.size(); j++) {
                map[i][n - j - 1= t[j];
            }
        }
    }
    else if (d == 2) {//아래
        for (int i = 0; i < n; i++) {
            t.clear();
 
            for (int j = n - 1; j >= 0; j--) {
                if (map[j][i] != 0)
                    t.push_back(map[j][i]);
                map[j][i] = 0;
            }
            if (t.size() == 0)
                continue;
            for (int j = 0; j < t.size() - 1; j++) {
                if (t[j] == t[j + 1]) {
                    t[j] *= 2;
                    t.erase(t.begin() + (j + 1));
                }
            }
            for (int j = 0; j < t.size(); j++) {
                map[n - j - 1][i] = t[j];
            }
        }
    }
    else if (d == 3) {//위
        for (int i = 0; i < n; i++) {
            t.clear();
 
            for (int j = 0; j < n; j++) {
                if (map[j][i] != 0)
                    t.push_back(map[j][i]);
                map[j][i] = 0;
            }
            if (t.size() == 0)
                continue;
            for (int j = 0; j < t.size() - 1; j++) {
                if (t[j] == t[j + 1]) {
                    t[j] *= 2;
                    t.erase(t.begin() + (j + 1));
                }
            }
            for (int j = 0; j < t.size(); j++) {
                map[j][i] = t[j];
            }
        }
    }
}
int answer = 0;
 
void solution(int cnt) {
    if (cnt == 5) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                answer = answer > map[i][j] ? answer : map[i][j];
            }
        }
        return;
    }
    vector<vector<int>> tmp(n, vector<int>(n));
    tmp = map;
    for (int i = 0; i < 4; i++) {
        func(i);
        solution(cnt + 1);
        map = tmp;
    }
    return;
}
 
int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> n;
    map.assign(n, vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> map[i][j];
        }
    }
    //freopen("Input.txt", "r", stdin);
    solution(0);
    cout << answer;
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter