문제풀이/구현

○ [파이썬] [구현] 백준 14499 주사위 굴리기

승무_ 2023. 3. 25. 13:50

문제

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

코드

import sys
input=sys.stdin.readline

def turn(dir):
    a, b, c, d, e, f = dice[0], dice[1], dice[2], dice[3], dice[4], dice[5]
    if dir == 1: #동
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = d, b, a, f, e, c

    elif dir == 2: #서
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = c, b, f, a, e, d

    elif dir == 3: #북
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = e, a, c, d, f, b

    else: #남
        dice[0], dice[1], dice[2], dice[3], dice[4], dice[5] = b, f, c, d, a, e


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

n,m,x,y,k=map(int, input().split())
array=[list(map(int, input().split())) for _ in range(n)]
direction=list(map(int, input().split()))

dice = [0] * 6
for dir in direction:

    if 0<=x+dx[dir]<n and 0<=y+dy[dir]<m:
        x+=dx[dir]
        y+=dy[dir]
        turn(dir)
        if array[x][y]==0:
            array[x][y]=dice[-1]
        else:
            dice[-1]=array[x][y]
            array[x][y]=0
        print(dice[0])

생각 정리

주사위를 돌릴때, 인덱스의 변화를 생각하기 까다로웠다.