본문 바로가기
Algorithm/삼성 SW 역량 테스트 기출 문제

[Baekjoon][14499] 주사위 굴리기

by 어발 2022. 2. 14.

출처 - 백준사이트

14499번: 주사위 굴리기

나의 풀이

이번 문제는 난이도가 높지는 않다.
주사위를 동서남북으로 움직일때의 규칙은 방향이 같다면 동일하기 때문에, 해당 규칙을 찾아서 주사위를 이동시킨 후 조건을 따지면 된다.

#include <iostream>

#define MAX    20

const int dy[4] = {0, 0, -1, 1};
const int dx[4] = {1, -1, 0, 0};

int N, M, K;
int x, y;
int map[MAX][MAX];
int dice[6] = {0, };

int Moving(int direction) {
    int temp = 0;
    int ny = y + dy[direction-1];
    int nx = x + dx[direction-1];

    if (0<=ny && ny<N && 0<=nx && nx<M) {
        y = ny;
        x = nx;

        switch(direction) {
        case 1:
            temp = dice[1];
            dice[1] = dice[4];
            dice[4] = dice[3];
            dice[3] = dice[5];
            dice[5] = temp;
            break;
        case 2:        
            temp = dice[1];
            dice[1] = dice[5];
            dice[5] = dice[3];
            dice[3] = dice[4];
            dice[4] = temp;
            break;
        case 3:
            temp = dice[0];
            dice[0] = dice[1];
            dice[1] = dice[2];
            dice[2] = dice[3];
            dice[3] = temp;
            break;
        case 4:
            temp = dice[0];
            dice[0] = dice[3];
            dice[3] = dice[2];
            dice[2] = dice[1];
            dice[1] = temp;
            break;
        default:
            break;
        }        

        if (map[y][x] == 0)
            map[y][x] = dice[3];
        else {
            dice[3] = map[y][x];
            map[y][x] = 0;
        }

        return 1;
    }

    return -1;
}

int main(void) {
    int order = 0;

    scanf("%d %d %d %d %d", &N, &M, &y, &x, &K);

    for (int n=0 ; n<N ; n++) {
        for (int m=0 ; m<M ; m++)
            scanf("%d", &map[n][m]);
    }

    for (int i=0 ; i<K ; i++) {
        scanf("%d", &order);

        if (Moving(order) == 1)
            printf("%d\n", dice[1]);
    }

    return 0;
}
728x90

댓글