백준
1. Python
import sys
input = sys.stdin.readline
n, m, x, y, k = map(int, input().split())
dice = [0] * 6
board = [list(map(int, input().split())) for _ in range(n)]
mlist = list(map(int, input().split()))
dx = [0, 0, -1, 1]
dy = [1, -1, 0, 0]
for i in range(k):
d = mlist[i] - 1
nx = x + dx[d]
ny = y + dy[d]
if 0 <= nx < n and 0 <= ny < m:
if d == 0:
dice[0], dice[2], dice[3], dice[5] = dice[3], dice[0], dice[5], dice[2]
elif d== 1:
dice[0], dice[2], dice[3], dice[5] = dice[2], dice[5], dice[0], dice[3]
elif d == 2:
dice[0], dice[1], dice[4], dice[5] = dice[4], dice[0], dice[5], dice[1]
else:
dice[0], dice[1], dice[4], dice[5] = dice[1], dice[5], dice[0], dice[4]
if board[nx][ny] == 0:
board[nx][ny] = dice[5]
else:
dice[5] = board[nx][ny]
board[nx][ny] = 0
x, y = nx, ny
print(dice[0])