Sneak

2022. 7. 24. 14:40Algorithm

    목차
반응형

 

N = int(input())
K = int(input())


# 0: empty
# 1: apple
grid = [[0]*N for _ in range(N)]

for _ in range(K):
    y, x = map(int, input().split())
    grid[y - 1][x - 1] = 1


next_dirs = {
    (0, 1): {       # R
        'L': (-1, 0),
        'D': (1, 0)
    },
    (0, -1): {      # L
        'L': (1, 0),
        'D': (-1, 0)
    },
    (1, 0): {       # D
        'L': (0, 1),
        'D': (0, -1)
    },
    (-1, 0): {      # U
        'L': (0, -1),
        'D': (0, 1)
    }
}

L = int(input())
moves = []

for _ in range(L):
    sec, rot = input().split()
    moves += (int(sec), rot),


cy = cx = 0
oy, ox = 0, 1
sneak = [(0, 0)]
sec = 1

while True:
    ny = cy + oy
    nx = cx + ox

    if not (0 <= ny < N and 0 <= nx < N) or (ny, nx) in sneak:
        break

    sneak += (ny, nx),

    if 0 == grid[ny][nx]:
        sneak.pop(0)
    elif 1 == grid[ny][nx]:
        grid[ny][nx] = 0

    if moves and moves[0][0] == sec:
        oy, ox = next_dirs[(oy, ox)][moves[0][1]]
        moves.pop(0)

    cy = ny
    cx = nx
    sec += 1

print(sec)
반응형