이 문제는 난이도가 굉장히 낮은데..

주사위를 굴렸을 때.. 전개도에서 인덱스만 잘 스왑해주면 끝나는 문제입니다.

핵심 함수는 dice_move()입니다.



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
#include <stdio.h>
#include <iostream>
 
#define MAX_SIZE 25
 
using namespace std;
 
int m, n;
int sx, sy;
int k;
int map[MAX_SIZE][MAX_SIZE];
int cmd[1000];
int dx[4= { 00-11 };//동서북남
int dy[4= { 1-100 };
int dice[6= {0, };
 
void dice_move(int d){
    switch (d){
    case 0://동
        swap(dice[2], dice[0]);
        swap(dice[0], dice[3]);
        swap(dice[3], dice[5]);
        break;
    case 1://서
        swap(dice[3], dice[0]);
        swap(dice[0], dice[2]);
        swap(dice[2], dice[5]);
        break;
    case 2://북
        swap(dice[4], dice[5]);
        swap(dice[0], dice[4]);
        swap(dice[1], dice[0]);
        break;
    case 3://남
        swap(dice[1], dice[0]);
        swap(dice[0], dice[4]);
        swap(dice[4], dice[5]);
        break;
    }
 
}
 
void input(){
    scanf("%d %d"&m, &n);
    scanf("%d %d"&sx, &sy);
    scanf("%d"&k);
    for (int i = 0; i < m; i++){
        for (int j = 0; j < n; j++){
            scanf("%d"&map[i][j]);
        }
    }
 
    for (int i = 0; i < k; i++){
        int d;
        scanf("%d"&d);
        cmd[i] = d - 1;
    }
 
}
 
void solve(){
    int x = sx;
    int y = sy;
 
    for (int i = 0; i < k; i++){
        int nx = x + dx[cmd[i]];
        int ny = y + dy[cmd[i]];
 
        if (nx < 0 || ny < 0 || nx >= m || ny >= n) continue;
        
        dice_move(cmd[i]);//아래는 5번인덱스; 위는 0
 
        if (map[nx][ny] == 0){
            map[nx][ny] = dice[5];
        }
        else{
            dice[5= map[nx][ny];
            map[nx][ny] = 0;
        }
        printf("%d\n", dice[0]);
 
        x = nx;
        y = ny;
    }
 
 
}
 
int main(){
    input();
    solve();
 
 
    return 0;
    
}
cs


'알고리즘 > 구현' 카테고리의 다른 글

[3474] 교수가 된 현우  (0) 2017.07.13
[5527] 전구 장식  (0) 2017.07.05
[5624] 좋은 수  (0) 2017.04.06
[3190] 뱀  (0) 2017.04.05
[1952] 달팽이2  (0) 2017.04.04

+ Recent posts