본문 바로가기
Algorithm(CodeTree, Python)/Simulation

[코드트리] 주사위 던지기 Python

by kurooru 2023. 1. 21.
# n, m, r, c 입력
n, m, r, c = map(int, input().split())
r -= 1
c -= 1
# orders
orders = list(input().split())

# 함수들
# in_range(x, y)
def in_range(x, y):
    return 0 <= x < n and 0 <= y < n

# simulate(curr_order)
def simulate(curr_order):
    
    # 전역 변수 선언
    global r, c, top, bottom, right, left, front, back

    # 왼쪽으로 굴리고, 범위 내에 있으면
    if curr_order == 'L' and in_range(r, c-1):
        
        # temps
        temp_top = right
        temp_left = top
        temp_bottom = left
        temp_right = bottom

        # 위치 바꿔주기
        r, c = r, c-1

        # 주사위 숫자 바꿔주기
        top = temp_top
        left = temp_left
        bottom = temp_bottom
        right = temp_right

        # 찍어주기
        grid[r][c] = bottom

    # 오른으로 굴리고, 범위 내에 있으면
    elif curr_order == 'R' and in_range(r, c+1):
        
        # temps
        temp_top = left
        temp_left = bottom
        temp_bottom = right
        temp_right = top

        # 위치 바꿔주기
        r, c = r, c+1

        # 주사위 숫자 바꿔주기
        top = temp_top
        left = temp_left
        bottom = temp_bottom
        right = temp_right

        # 찍어주기
        grid[r][c] = bottom

    # 아래쪽으로 굴리고, 범위 내에 있으면
    elif curr_order == 'D' and in_range(r+1, c):
        
        # temps
        temp_top = back
        temp_front = top
        temp_bottom = front
        temp_back = bottom

        # 위치 바꿔주기
        r, c = r+1, c

        # 주사위 숫자 바꿔주기
        top = temp_top
        front = temp_front
        bottom = temp_bottom
        back = temp_back

        # 찍어주기
        grid[r][c] = bottom

    # 위쪽으로 굴리고, 범위 내에 있으면
    elif curr_order == 'U' and in_range(r-1, c):
        
        # temps
        temp_top = front
        temp_front = bottom
        temp_bottom = back
        temp_back = top

        # 위치 바꿔주기
        r, c = r-1, c

        # 주사위 숫자 바꿔주기
        top = temp_top
        front = temp_front
        bottom = temp_bottom
        back = temp_back

        # 찍어주기
        grid[r][c] = bottom

# 설계
# grid
grid = [
    [0] * n
    for _ in range(n)
]

# top, bottom, front, back, right, left
top, bottom, front, back, right, left = 1, 6, 2, 5, 3, 4

# 첫 위치에 bottom 적어주기
grid[r][c] = bottom

# 주문 수행
for order in orders:
    # simualte
    simulate(order)

# ans
ans = 0

# ans 구하기
for i in range(n):
    for j in range(n):
        ans += grid[i][j]

# 출력
print(ans)