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

코드
def answer():
global n, m, y, x, k, board, operations
BOTTOM = 6
UP = 1
dirs = [0, (0, 1), (0, -1), (-1, 0), (1, 0)]
d = [0, 0, 0, 0, 0, 0, 0]
ds = [
0,
[0, 4, 2, 1, 6, 5, 3], # 동
[0, 3, 2, 6, 1, 5, 4], # 서
[0, 5, 1, 3, 4, 6, 2], # 북
[0, 2, 6, 3, 4, 1, 5] # 남
]
for op in operations:
_y, _x = y, x # 백업
y, x = (y+dirs[op][0], x+dirs[op][1])
if y < 0 or y >= n or x < 0 or x >= m:
y, x = _y, _x # 원상복귀
continue
d = [d[i] for i in ds[op]]
if board[y][x] == 0: board[y][x] = d[BOTTOM]
else:
d[BOTTOM] = board[y][x]
board[y][x] = 0
print(d[UP])
n, m, y, x, k = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(n)]
operations = list(map(int, input().split()))
answer()