목차

ps
링크acmicpc.net/…
출처BOJ
문제 번호3190
문제명
레벨골드 5
분류

구현

시간복잡도O(n)
인풋사이즈n<=10000
사용한 언어Python
제출기록32508KB / 100ms
최고기록60ms
해결날짜2022/04/26
태그

[라이] 리스트/배열/연결 리스트

풀이

코드

"""Solution code for "BOJ 3190. 뱀".

- Problem link: https://www.acmicpc.net/problem/3190
- Solution link: http://www.teferi.net/ps/problems/boj/3190

Tags: [Implementation]
"""

import collections

APPLE = 'A'
SNAKE = 'S'
EMPTY = 'E'
DELTAS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
INF = float('inf')


def main():
    N = int(input())
    grid = [[EMPTY] * N for _ in range(N)]
    K = int(input())
    for _ in range(K):
        apple_r, apple_c = [int(x) for x in input().split()]
        grid[apple_r - 1][apple_c - 1] = APPLE
    L = int(input())
    turns = []
    for _ in range(L):
        X, C = input().split()
        turns.append((int(X), C))

    turns.append([INF, 'X'])
    r, c = 0, 0
    grid[r][c] = SNAKE
    queue = collections.deque([(r, c)])
    direction = 0
    dr, dc = DELTAS[direction]
    t = 0
    for X, C in turns:
        while t < X:
            t += 1
            r, c = r + dr, c + dc
            if not (0 <= r < N and 0 <= c < N) or grid[r][c] == SNAKE:
                break
            if grid[r][c] == EMPTY:
                tail_r, tail_c = queue.popleft()
                grid[tail_r][tail_c] = EMPTY
            grid[r][c] = SNAKE
            queue.append((r, c))
        else:
            direction = (direction + (1 if C == 'D' else -1)) % 4
            dr, dc = DELTAS[direction]
            continue
        break

    print(t)