얼렁뚱땅 백준 문제풀이

[백준 문제풀이] 얼렁뚱땅 14499 주사위 굴리기 풀이

MOSTAR 2022. 10. 11. 01:28

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

 

주사위 돌리는거 도대체 어떻게 이런 아이디어가 나올까?

좀 더 고민했어야 했는데 넘 졸려서 으앙 잔다 뿅

 

n, m, x, y, k = map(int,input().split())
arr = [list(map(int,input().split())) for i in range(n)]
to_do_list = list(map(int,input().split()))

#동 서 북 남
dx = [0, 0, 0, -1, 1]
dy = [0, 1, -1, 0, 0]


def turn_dice(dir) :
    # 동 서 북 남
    global dice
    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


dice = [0, 0, 0, 0, 0, 0]
ct = 1
for i in range(k) :
    to_do = to_do_list[i]
    # 주사기가 움직일 수 있나
    nx = x + dx[to_do]
    ny = y + dy[to_do]
    if 0<=nx<n and 0<=ny<m :
        turn_dice(to_do)
        if arr[nx][ny] == 0 :
            arr[nx][ny] = dice[-1]
        else :
            dice[-1] = arr[nx][ny]
            arr[nx][ny] = 0
        x = nx
        y = ny
        print(dice[0])