Python/구현 BOJ-14499 주사위굴리기

[Python/구현] BOJ-14499 주사위굴리기

방향설정주사위 위치 설정만 하면 끝나는 문제이다.

이해가 잘 안된다면 동서남북으로 주사위를 돌렸을때 전개도를 그려보면 된다.

방향 : 동=0 / 서=1 / 북=2 / 남=3

주사위 위치 : 위=0 /북=1 /동=2 /서=3 /남=4 /아래=5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 방향 : 동=0 / 서=1 / 북=2 / 남=3
dx = [0,0,-1,1]
dy = [1,-1,0,0]

# 첫 주사위는 모든 면이 0 이로 기재 되어있음
dice = [0]*6

N, M, x, y, K = map(int,input().split()) # 세로 / 가로 / x/ y/ 명령수
a = [list(map(int,input().split())) for _ in range(N)] # 지도에 쓰여있는 숫자
direction = list(map(int,input().split())) # 주사위 방향

for i in range(K) :
    dir = direction[i]-1
    nx = x + dx[dir]    
    ny = y + dy[dir]    
    
    if not 0 <= nx < N or not 0 <= ny < M :
        continue
    
	# 주사위 위치 : 위=0 /북=1 /동=2 /서=3 /남=4 /아래=5
    # 좌/우로 돌릴경우 북,남 위치 동일
    # 남/북으로 돌릴경우 서,동 위치 동일
    if dir == 0 :
        dice[0],dice[2],dice[3],dice[5] = dice[3], dice[0], dice[5],dice[2]
    elif dir == 1 :
        dice[0],dice[2],dice[3],dice[5] = dice[2], dice[5], dice[0],dice[3]
    elif dir == 2 :
        dice[0],dice[1],dice[4],dice[5] = dice[4], dice[0], dice[5],dice[1]
    elif dir == 3 :
        dice[0],dice[1],dice[4],dice[5] = dice[1], dice[5], dice[0],dice[4]
    
    if a[nx][ny] == 0 :
        a[nx][ny] = dice[5]
    else :
        dice[5] = a[nx][ny]
        a[nx][ny] = 0
    
    x,y=nx,ny
    print(dice[0])

Reference

문제 : https://www.acmicpc.net/problem/14499