본문 바로가기
백준

[python] 3190. 뱀

by DylanMsK 2019. 7. 24.

문제 출처

3190. 뱀

 

풀이


N = int(input())
K = int(input())
apples = [tuple(map(int, input().split())) for _ in range(K)]
L = int(input())
cmd = [input().split() for _ in range(L)]

directions = {
    'E': (0, 1),
    'S': (1, 0),
    'W': (0, -1),
    'N': (-1, 0)
}
rotation = {
    'E': {'L': 'N', 'D': 'S'},
    'S': {'L': 'E', 'D': 'W'},
    'W': {'L': 'S', 'D': 'N'},
    'N': {'L': 'W', 'D': 'E'},
}
cnt = 0
snake = [(0, 0)]
d = 'E'

while 1:
    if cmd and cnt == int(cmd[0][0]):
        _, curve = cmd.pop(0)
        d = rotation[d][curve]
    cnt += 1

    y, x = snake[0]
    ny, nx = y+directions[d][0], x+directions[d][1]
    if (ny+1, nx+1) in apples:
        apples.pop(apples.index((ny+1, nx+1)))
    else:
        if (ny, nx) in snake[1:]:
            break
        snake = snake[:-1]
    if nx < 0 or nx >= N or ny < 0 or ny >= N:
        break
    if (ny, nx) in snake:
        break
    snake  = [(ny, nx)] + snake

print(cnt)

'백준' 카테고리의 다른 글

[python] 16234. 인구 이동  (0) 2019.07.27
[python] 17140. 이차원 배열과 연산  (0) 2019.07.24
[python] 14891. 톱니바퀴  (0) 2019.07.24
[python] 15685. 드래곤 커브  (0) 2019.07.24
[python] 14888. 연산자 끼워넣기  (1) 2019.07.24