Post

[BOJ] 12100 - 2048(Easy)

[백준] 12100 - 2048(Easy)

처음 구현할 때 한번의 스와이프에도 블록이 여러번 합쳐질 수 있도록 구현해서 틀렸습니다를 받았다. 문제 이해가 가장 중요하다는 것을 늘 깨닫는다.

소스코드
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
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <string.h>
using namespace std;

int mp[25][25], n;

int swipe(int direction, int cnt, int tmp[][25]) {
    if (cnt > 5) return 0;
    int buf[25][25] = { 0, }, rtn = 0;
    for (int i = 0; i < n; i++) {
        int now = 0, val = 0;
        for (int j = 0; j < n; j++) {
            int x, y, nx, ny;
            if (direction == 0) {
                x = i; y = j;
                nx = i; ny = now;
            }
            else if (direction == 1) {
                x = i; y = n - j - 1;
                nx = i, ny = n - now - 1;
            }
            else if (direction == 2) {
                x = j; y = i;
                nx = now; ny = i;
            }
            else {
                x = n - j - 1; y = i;
                nx = n - now - 1; ny = i;
            }

            if (tmp[x][y] == 0);
            else if (val == 0) {
                val = tmp[x][y];
                rtn = max(rtn, val);
            }
            else if (tmp[x][y] == val) {
                val *= 2;
                rtn = max(rtn, val);
                buf[nx][ny] = val;
                now++;
                direction == 0 ? ny++ : direction == 1 ? ny-- : direction == 2 ? nx++ : nx--;
                val = 0;
            }
            else {
                buf[nx][ny] = val;
                now++;
                direction == 0 ? ny++ : direction == 1 ? ny-- : direction == 2 ? nx++ : nx--;
                val = tmp[x][y];
                rtn = max(rtn, val);
            }

            if (j == n - 1) {
                buf[nx][ny] = val;
            }
        }
    }
    if (cnt != 5) {
        for (int i = 0; i < 4; i++) {
            rtn = max(rtn, swipe(i, cnt + 1, buf));
        }
    }
    return rtn;
}

int go() {
    int rtn = 0;
    for (int i = 0; i < 4; i++) {
        rtn = max(rtn, swipe(i, 1, mp));
    }
    return rtn;
}

int main(void) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            scanf("%d", mp[i] + j);
        }
    }

    printf("%d", go());

    return 0;
}
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.