https://www.acmicpc.net/problem/14499
그대로 구현
def down():
cy, cx = curr
cy += 1
if not (0 <= cy < h and 0 <= cx < w):
return False
curr[0], curr[1] = cy, cx
swap(5, 6)
swap(1, 5)
swap(2, 1)
if board[cy][cx] == 0:
board[cy][cx] = dice[6]
else:
dice[6] = board[cy][cx]
board[cy][cx] = 0
return True
def up():
cy, cx = curr
cy -= 1
if not (0 <= cy < h and 0 <= cx < w):
return False
curr[0], curr[1] = cy, cx
swap(2, 1)
swap(1, 5)
swap(5, 6)
if board[cy][cx] == 0:
board[cy][cx] = dice[6]
else:
dice[6] = board[cy][cx]
board[cy][cx] = 0
return True
def right():
cy, cx = curr
cx += 1
if not (0 <= cy < h and 0 <= cx < w):
return False
curr[0], curr[1] = cy, cx
swap(1, 3)
swap(4, 1)
swap(6, 4)
if board[cy][cx] == 0:
board[cy][cx] = dice[6]
else:
dice[6] = board[cy][cx]
board[cy][cx] = 0
return True
def left():
cy, cx = curr
cx -= 1
if not (0 <= cy < h and 0 <= cx < w):
return False
curr[0], curr[1] = cy, cx
swap(1, 4)
swap(3, 1)
swap(6, 3)
if board[cy][cx] == 0:
board[cy][cx] = dice[6]
else:
dice[6] = board[cy][cx]
board[cy][cx] = 0
return True
def swap(i, j):
tmp = dice[i]
dice[i] = dice[j]
dice[j] = tmp
# init
import sys
ipt = sys.stdin.readline
opt = sys.stdout.write
h, w, y, x, k = map(int, ipt().split())
board = [list(map(int, ipt().split())) for _ in range(h)]
orders = list(map(int, ipt().split()))
dice = [0] * 7
curr = [y, x]
# start
for order in orders:
if order == 1:
if right():
opt(f'{dice[1]}\n')
elif order == 2:
if left():
opt(f'{dice[1]}\n')
elif order == 3:
if up():
opt(f'{dice[1]}\n')
elif order == 4:
if down():
opt(f'{dice[1]}\n')