티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve


요리조리 돌려봐야하는 주사위 문제보다는 친절한 문제이지만

주사위문제는 정말 뇌도 빙빙 돈다..


주사위를 쌓는 것도 친절하게 1번, 2번, .. 순서대로 쌓는다.

그렇다면 1번 주사위가 기준이 되고 

1번 주사위에서 윗면 숫자를 정해주면 그에 따라 총총 주사위를 쌓아주면 된다.


먼저, N개의 주사위 정보를 저장할 공간이 필요하고

각 맞은편 인덱스를 저장할 공간도 필요하다.

밑면이 정해지면 이 맞은편 숫자를 윗면으로 설정해야하기 때문이다.


#. Code


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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BOJ2116 {
 
    static int N, dice[][], res;
    static int[] opp = { 0645231 };
    static boolean[][] isAdhere;
 
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
 
        N = Integer.parseInt(br.readLine());
        dice = new int[N][7];
        
        // Make dice
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            
            for (int j = 1; j <= 6; j++) {
                dice[i][j] = Integer.parseInt(st.nextToken());
            }
        }
 
        pileDice();
        
        System.out.println(res);
    }
 
    private static void pileDice() {
 
        int up = 0;
        // 1번 주사위의 윗 면이 1 ~ 6일 경우
        for (int num = 1; num <= 6; num++) {
 
            isAdhere = new boolean[N][7];
            for (int f = 1; f <= 6; f++) {
                if(dice[0][f] == num) {
                    up = dice[0][f];
                    
                    isAdhere[0][f] = isAdhere[0][opp[f]] = true;
                    break;
                }
            }
        
            // 1번 주사위의 윗 면이 정해지면 그에 맞게 주사위 쌓기
            for (int i = 1; i < N; i++) {
                
                for (int f = 1; f <= 6; f++) {
                    if(dice[i][f] == up) {
                        up = dice[i][opp[f]];
                        
                        isAdhere[i][f] = isAdhere[i][opp[f]] = true;
                        break;
                    }
                }
            }
            
            // 주사위가 다 쌓아지면
            res = Math.max(res, sumSide());
        }        
    }
 
    // 옆 면의 최대 값들을 합하기
    private static int sumSide() {
        
        int sum = 0;
        // 1번 주사위부터
        for (int i = 0; i < N; i++) {
            
            int max = 0;
            // 가장 큰 옆면 숫자를 찾고
            for (int j = 1; j <= 6; j++) {
                if(!isAdhere[i][j])
                    max = Math.max(max, dice[i][j]);
            }
            // 누적
            sum += max;            
        }
        
        return sum;
    }
 
}
 
cs


line 38) 1번 주사위의 윗면 숫자를 정해주자.

line 40) 쌓이면서 붙은 위, 아랫면은 체크를 해두자. 

  쌓인 주사위들의 한 옆면의 숫자의 합이 가장 큰 값을 찾아야 하니깐!

line 42~43) 해당 숫자 인덱스를 찾고, 그 인덱스의 맞은편 인덱스 숫자를 윗면으로 저장해둔다.

line 45) 윗면, 아랫면은 체크!


line 51) 1번 주사위가 놓였으니 2번~N번까지 주사위를 쌓아보자.

line 54) 마찬가지로 해당 숫자 인덱스를 찾고, 그 인덱스의 맞은편 인덱스 숫자를 윗면으로 저장해둔다.

line 45) 윗면, 아랫면은 체크!

line 64) 주사위가 다 쌓이면 쌓인 주사위들의 한 옆면의 숫자의 합을 구하러 가보자.


line 73) 1~N번 주사위를 돌면서

line 77~79) 가장 큰 옆면 숫자를 찾고

line 82) 누적해주자.

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday