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

코드
from collections import deque
n = int(input())
k = int(input())
board = [[0]*n for _ in range(n)]
사과, 벽 = ('A', '#')
for _ in range(k):
y, x = list(map(int, input().split()))
board[y-1][x-1] = 사과
board = [[벽]*n] + board + [[벽]*n]
height, width = (n+2, n+2)
for y in range(height): board[y] = [벽] + board[y] + [벽]
L = int(input())
moves = deque([list(map(str, input().split())) for _ in range(L)])
북, 동, 남, 서 = (0, 1, 2, 3)
dyx = [(-1, 0), (0, 1), (1, 0), (0, -1)]
왼쪽, 오른쪽 = ('L', 'D')
뱀 = deque([[1, 1, 1]]) # y, x, 남은생존시간
회전후머리방향 = {
왼쪽: [서, 북, 동, 남],
오른쪽: [동, 남, 서, 북]
}
def 몸통에닿았는가(y, x):
for i in range(len(뱀)):
if y == 뱀[i][0] and x == 뱀[i][1]: return True
return False
def 벽에닿았는가(y, x):
if board[y][x] == 벽: return True
return False
def 움직여(y, x, d):
ny, nx = (y+dyx[d][0], x+dyx[d][1])
# 벽이나 몸통에 닿았는지 체크
if 벽에닿았는가(ny, nx) or 몸통에닿았는가(ny, nx): return False
뱀.append([ny, nx, 뱀[-1][2]+1])
if board[ny][nx] != 사과:
for i in range(len(뱀)): 뱀[i][2] = 뱀[i][2]-1
else:
board[ny][nx] = 0 # 사과를 먹었으면 먹었다고 처리를 해줘야지!
if 뱀[0][2] == 0: 뱀.popleft()
return True
def answer():
현재머리방향 = 동
rightNow = 0
t, 회전방향 = moves.popleft()
while(True):
rightNow += 1
if 움직여(뱀[-1][0], 뱀[-1][1], 현재머리방향) is False: return rightNow
if rightNow == int(t):
현재머리방향 = 회전후머리방향[회전방향][현재머리방향]
if len(moves) > 0: t, 회전방향 = moves.popleft()
print(answer())