티스토리 뷰

https://www.acmicpc.net/problem/14499

 

14499번: 주사위 굴리기

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다. 둘째 줄부터 N개의 줄에 지

www.acmicpc.net

그냥 구현문제


 

const fs = require('fs');
const input = fs.readFileSync('./dev/stdin').toString().trim().split('\n');
const [N, M, X, Y, K] = input.shift().split(' ').map(Number);
const COMMANDS = input.pop().split(' ').map(Number);
let MAP = input.map((v) => v.split(' ').map(Number));

//      2
//    4 1 3
//      5
//      6

// 1 동
// 2 서
// 3 북
// 4 남
let x = X;
let y = Y;
let dice = [null, 0, 0, 0, 0, 0, 0];

let top = 6;
let bottom = 1;
let front = 5;
let back = 2;
let left = 4;
let right = 3;

const answer = [];
function move(dir) {
        const originTop = top;
        const originBottom = bottom;
        const originFront = front;
        const originBack = back;
        const originLeft = left;
        const originRight = right;

        switch (dir) {
                case 1: //동쪽
                        {
                                top = originLeft;
                                bottom = originRight;
                                left = originBottom;
                                right = originTop;
                        }
                        break;
                case 2: //서쪽
                        {
                                top = originRight;
                                bottom = originLeft;
                                left = originTop;
                                right = originBottom;
                        }
                        break;
                case 3: //북쪽
                        {
                                top = originFront;
                                bottom = originBack;
                                front = originBottom;
                                back = originTop;
                        }
                        break;
                case 4: //남쪽
                        {
                                top = originBack;
                                bottom = originFront;
                                front = originTop;
                                back = originBottom;
                        }
                        break;
        }
}

const dx = [0, 0, 0, -1, 1];
const dy = [0, 1, -1, 0, 0];

for (let i = 0; i < COMMANDS.length; i++) {
        const cmd = COMMANDS[i];
        const nx = x + dx[cmd];
        const ny = y + dy[cmd];

        if (nx < 0 || nx >= N || ny < 0 || ny >= M) {
                continue;
        } else {
                x = nx;
                y = ny;
        }

        move(cmd);
        const now = MAP[x][y];
        if (now === 0) {
                MAP[x][y] = dice[bottom];
        } else {
                dice[bottom] = MAP[x][y];
                MAP[x][y] = 0;
        }
        answer.push(dice[top]);
}

console.log(answer.join('\n'));
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/07   »
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
글 보관함