알고리즘/Python

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

_SIHA_ 2022. 3. 31. 03:24

📖  문제 링크

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

 

  최종 코드

from collections import deque

def solution():
    
    m, n, x, y, k = map(int, input().split())
    board = []

    for i in range(m):
        board.append(list(map(int, input().split())))

    order = list(map(int, input().split()))

    dice = [0,0,0,0,0,0]
    
    for i in range(k):
        temp_x, temp_y = x, y
        current.append(direction(order[i], x, y))
        x, y = current.popleft()
        if x<m and y<n and x>=0 and y>=0 :
            changeDice(dice, order[i])
            if board[x][y] == 0:
                board[x][y] = dice[5]
            else:
                dice[5] = board[x][y]
                board[x][y] = 0
            print(dice[0])
        else:
            x = temp_x
            y = temp_y  
               
        

def direction(go, x, y):
    #동 서 북 남
    dx = [0, 0, -1, 1]
    dy = [1, -1, 0, 0]
    
    if go == 1: # 동쪽
        x += dx[0]
        y += dy[0]
    elif go == 2: #서쪽
        x += dx[1]
        y += dy[1]
    elif go == 3: # 북쪽
        x += dx[2]
        y += dy[2]
    elif go == 4: # 남쪽
        x += dx[3]
        y += dy[3]
    
    return x, y

        
def changeDice(dice, go):
    
    if  go == 1:
        dice[0], dice[2], dice[3], dice[5] = dice[3], dice[0], dice[5], dice[2]
    elif go == 2:
        dice[0], dice[2], dice[3], dice[5] = dice[2], dice[5], dice[0], dice[3]
    elif go == 3:
        dice[0], dice[1], dice[4], dice[5] = dice[4], dice[0], dice[5], dice[1]
    else:
        dice[0], dice[1], dice[4], dice[5] = dice[1], dice[5], dice[0], dice[4]
    
   
    return dice
           
    
   
current = deque()
solution()