알고리즘

[백준/Python] 14499. 주사위 굴리기

스연 2023. 9. 25. 12:22
728x90

 

 

1. 백준 14499. 주사위 굴리기 (골4)

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

 

풀이

 

CODE

import sys

input = sys.stdin.readline

n, m, x, y, k = map(int, input().split())
board = []
for _ in range(n):
    board.append(list(map(int, input().split())))
direction = list(map(int, input().split()))
dice = [0] * 6

cur_bottom = 5
cur_top = 0

# 좌표 이동
dx = [0, 0, 0, -1, 1]
dy = [0, 1, -1, 0, 0]


def get_bottom_top(cur_d, cur_dice):
    if cur_d == 1:  # 동쪽 이동
        return [cur_dice[3], cur_dice[1], cur_dice[0], cur_dice[5], cur_dice[4], cur_dice[2]]
    elif cur_d == 2:  # 서쪽 이동
        return [cur_dice[2], cur_dice[1], cur_dice[5], cur_dice[0], cur_dice[4], cur_dice[3]]
    elif cur_d == 3:  # 북쪽 이동
        return [cur_dice[4], cur_dice[0], cur_dice[2], cur_dice[3], cur_dice[5], cur_dice[1]]
    elif cur_d == 4:  # 남쪽 이동
        return [cur_dice[1], cur_dice[5], cur_dice[2], cur_dice[3], cur_dice[0], cur_dice[4]]


for d in direction:
    # 현재 x를 기준으로 좌표값 갱신
    nx = x + dx[d]
    ny = y + dy[d]
    # print(d, nx, ny)
    # 범위 내의 이동이라면
    if 0 <= nx < n and 0 <= ny < m:
        dice = get_bottom_top(d, dice)
        # 밑 바닥 or 지도 갱신
        if board[nx][ny] == 0:
            board[nx][ny] = dice[cur_bottom]
        else:
            dice[cur_bottom] = board[nx][ny]
            board[nx][ny] = 0
        # print('현재 주사위: ', dice)
        # print('갱신된 board', board)

        # 현재 top 출력
        print(dice[cur_top])
        x = nx
        y = ny
  • 야호 나의 첫 골4 솔브...!!!
  • 잘 구현 했는데 문제 조건을 좀 헷갈려서 시간이 오래 걸렸다. 그거 빼고는 꽤 빨리 감잡고 구현한 것 같아서 뿌듯하다.

 

728x90