Created
November 26, 2019 07:00
-
-
Save inspirit941/21bbc48099116467f371906ea74c0572 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
N = int(sys.stdin.readline()) | |
K = int(sys.stdin.readline()) | |
maps = [[0 for _ in range(N)] for _ in range(N)] | |
for _ in range(K): | |
y, x = map(int, sys.stdin.readline().split()) | |
# 사과 위치 표시하기. | |
maps[y-1][x-1] = 2 | |
# 방향이 바뀌어야 하는 시간대 저장하기. | |
change = {} | |
L = int(sys.stdin.readline()) | |
for _ in range(L): | |
time, letter = sys.stdin.readline().split() | |
change[int(time)] = letter | |
# 처음 위치, 처음 움직일 방향 설정 | |
maps[0][0] = 1 | |
move = (0,1) | |
# 방향 바꾸는 함수 | |
def change_dir(move, Letter): | |
y, x = move | |
# x축 기준으로 이동할 때 | |
if y == 0: | |
if Letter == 'D': | |
if x > 0: | |
return (1,0) | |
else: | |
return (-1,0) | |
else: | |
if x > 0: | |
return (-1,-0) | |
else: | |
return (1,0) | |
# y축 기준으로 이동할 때 | |
elif x == 0: | |
if Letter == 'D': | |
if y > 0: | |
return (0,-1) | |
else: | |
return (0,1) | |
else: | |
if y > 0: | |
return (0,1) | |
else: | |
return (0,-1) | |
time = 0 | |
y, x = 0, 0 | |
# 뱀 좌표 저장하기 | |
snake = deque() | |
snake.append((y, x)) | |
while True: | |
time += 1 | |
dy, dx = move | |
ny, nx = y + dy, x + dx | |
# 보드 위에 있을 때 | |
if 0 <= ny < N and 0 <= nx < N: | |
# 사과인 경우 | |
if maps[ny][nx] == 2: | |
snake.appendleft((ny, nx)) | |
maps[ny][nx] = 1 | |
# 빈 칸인 경우 | |
elif maps[ny][nx] == 0: | |
maps[ny][nx] = 1 | |
snake.appendleft((ny, nx)) | |
# 맨 뒷꼬리 부분을 빼낸다. | |
taily, tailx = snake.pop() | |
maps[taily][tailx] = 0 | |
# 자기 몸에 부딪혔으므로 더 이상 진행하지 않는다. | |
elif maps[ny][nx] == 1: | |
break | |
y, x = ny, nx | |
# 방향을 변환해야 하는 시간인 경우 | |
if time in change: | |
move = change_dir(move, change[time]) | |
# 보드를 벗어난 경우이므로 반복문을 종료한다. | |
else: | |
break | |
print(time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment