티스토리 뷰

반응형


#. 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


체크해야 할 조건은

* 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥 면에 쓰여 있는 수가 칸에 복사

  0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥 면으로 복사되며, 칸에 쓰여 있는 수는 0

* 주사위는 지도의 바깥으로 이동시킬 수 없음

  만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 됨


이 정도(?)일 것 같다.


동작은

1. 주사위를 굴린다.

1-1. 주사위가 바깥으로 나갈 경우 무시

2. 주사위의 각 위칫값의 변화 (up, down, right, left, front, back)

3. 이동한 칸에 쓰여 있는 수에 따른 동작


#. 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BOJ14499 {
    
    static int N, M, x, y, K, map[][], commands[];
    static int[] dr = {000-11}, dc = {01-100}; // 동서북남
    static int[] dice;
    /* dice figure
     *      2
     *   4 (1) 3
     *      5
     *      6
     */
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        N = Integer.parseInt(st.nextToken()); // 세로
        M = Integer.parseInt(st.nextToken()); // 가로
        x = Integer.parseInt(st.nextToken()); // 주사위 x
        y = Integer.parseInt(st.nextToken()); // 주사위 y
        K = Integer.parseInt(st.nextToken()); // 명령의 개수
        
        map = new int[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken()); 
            }
        }
        
        commands = new int[K];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < K; i++) {
            commands[i] = Integer.parseInt(st.nextToken());
        }
        
        dice = new int[7];
        System.out.println(process());
    }
 
    private static StringBuilder process() {
 
        StringBuilder sb = new StringBuilder();
        
        int res = 0;
        for (int i = 0; i < K; i++) {
            res = command(commands[i]);
            // 주사위가 바깥으로 이동할 경우
            if(res == -1continue;
            sb.append(res + "\n");
        }
        
        return sb;
    }
 
    private static int command(int dir) {
 
        // 주사위를 굴리고
        x += dr[dir];
        y += dc[dir];
 
        // 주사위가 바깥으로 나갈 경우 
        if(x < 0 || y < 0 || x >= N || y >=M) {
            x -= dr[dir];
            y -= dc[dir];
            
            return -1;
        }
        
        // 주사위 위치의 변화
        int[] tmpDice = new int[7];        
        for (int i = 0; i < 7; i++) {
            tmpDice[i] = dice[i];
        }
        switch(dir) {
        case 1// 동
            dice[1= tmpDice[4]; dice[3= tmpDice[1]; dice[4= tmpDice[6]; dice[6= tmpDice[3];
            break;
        case 2// 서
            dice[1= tmpDice[3]; dice[3= tmpDice[6]; dice[4= tmpDice[1]; dice[6= tmpDice[4];
            break;
        case 3// 북
            dice[1= tmpDice[5]; dice[2= tmpDice[1]; dice[5= tmpDice[6]; dice[6= tmpDice[2];
            break;
        case 4// 남
            dice[1= tmpDice[2]; dice[2= tmpDice[6]; dice[5= tmpDice[1]; dice[6= tmpDice[5];
            break;
        }
        
        // 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사 
        if(map[x][y] == 0) map[x][y] = dice[6];
        // 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0
        else {
            dice[6= map[x][y];
            map[x][y] = 0;
        }
        
        return dice[1];
    }
    
}
cs


#. Code_v2


최적화 ver.


처음에는 함수 단위로 구현하느라 배열을 활용했는데

굳이 배열을 활용하지 않고 변수 재활용으로 해결할 수 있다.


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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BOJ14499_v2 {
    
    static int N, M, x, y, K, map[][];
    static int[] dr = {000-11}, dc = {01-100}; // 동서북남
    /* dice figure
     *      b
     *   l (u) r
     *      f
     *      d      t
     */
    
    public static void main(String[] args) throws IOException {
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        StringBuilder sb = new StringBuilder();
        
        N = Integer.parseInt(st.nextToken()); // 세로
        M = Integer.parseInt(st.nextToken()); // 가로
        x = Integer.parseInt(st.nextToken()); // 주사위 x
        y = Integer.parseInt(st.nextToken()); // 주사위 y
        K = Integer.parseInt(st.nextToken()); // 명령의 개수
        
        map = new int[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                map[i][j] = Integer.parseInt(st.nextToken()); 
            }
        }
        
        int u, l, r, f, b, d, t;
        u = l = r = f = b = d = t =0;
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < K; i++) {
            int command = Integer.parseInt(st.nextToken());
            // 주사위를 굴리고
            int xx = x + dr[command];
            int yy = y + dc[command];
            // 주사위가 바깥으로 나갈 경우 
            if(xx < 0 || yy < 0 || xx >= N || yy >=M) continue;
            // 주사위 위치의 변화
            switch(command) {
            case 1: t = u; u = l; l = d; d = r; r = t; // 동
                break;
            case 2: t = u; u = r; r = d; d = l; l = t; // 서
                break;
            case 3: t = u; u = f; f = d; d = b; b = t; // 북
                break;
            case 4: t = u; u = b; b = d; d = f; f = t; // 남
                break;
            }
            // 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사 
            if(map[xx][yy] == 0) map[xx][yy] = d;
            // 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0
            else {
                d = map[xx][yy];
                map[xx][yy] = 0;
            }
            x = xx;
            y = yy;
            
            sb.append(u + "\n");
        }
        
        System.out.println(sb);
    }
    
}
cs



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