https://www.acmicpc.net/problem/3190
from collections import deque
N = int(input())
apple_num = int(input())
apple_arr = []
for i in range(apple_num):
apple_arr.append(list(map(int, input().split())))
direction_num = int(input())
direction_arr = []
for i in range(direction_num):
num, char = input().split()
direction_arr.append((int(num), char))
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
direction = 0
snake = deque([(0, 0)])
count = 0
def check_valid_range(dx, dy):
return (0 <= dx < N and 0 <= dy < N) and (dx, dy) not in snake
def change_direction(dir):
global direction
if dir == 'D':
direction = (direction + 1) % 4
elif dir == 'L':
direction = (direction - 1) % 4
for dir in direction_arr:
sec = dir[0]
stop = False
for i in range(sec):
count += 1
now = snake[-1]
new_x = now[0] + dx[direction]
new_y = now[1] + dy[direction]
if (not check_valid_range(new_x, new_y)):
stop = True
break snake.append((new_x, new_y))
if [new_x + 1, new_y + 1] in apple_arr:
apple_arr.remove([new_x + 1, new_y + 1])
else:
snake.popleft()
if (stop == True):
break
change_direction(dir[1])
print(count)
from collections import deque
N = int(input())
apple_num = int(input()) # 사과 입력
apple_arr = []
for i in range(apple_num):
apple_arr.append(list(map(int, input().split())))
direction_num = int(input()) # 방향 지시 입력
direction_arr = []
for i in range(direction_num):
num, char = input().split()
direction_arr.append((int(num), char))
direction_idx = 0
dx = [0, 1, 0, -1] # 열, 행이므로 반대로 설정
dy = [1, 0, -1, 0]
direction = 0
snake = deque([(0, 0)]) # 뱀이 있는 좌표
count = 0
def change_direction(dir):
global direction
if dir == 'D':
direction = (direction + 1) % 4
elif dir == 'L':
direction = (direction - 1) % 4
while True:
count += 1
now = snake[-1]
new_x = now[0] + dx[direction]
new_y = now[1] + dy[direction]
if not (0 <= new_x < N and 0 <= new_y < N) or (new_x, new_y) in snake:
break
snake.append((new_x, new_y))
if [new_x + 1, new_y + 1] in apple_arr:
apple_arr.remove([new_x + 1, new_y + 1])
else:
snake.popleft()
if direction_idx < len(direction_arr) and count == direction_arr[direction_idx][0]:
change_direction(direction_arr[direction_idx][1])
direction_idx += 1
print(count)