반응형
https://www.acmicpc.net/problem/3190
import collections
n = int(input())
k = int(input())
apples = collections.defaultdict(int)
for i in range(k):
x, y = map(int, input().split())
x -= 1
y -= 1
apples[1000*x+y] = 1
t = int(input())
turns = {}
for i in range(t):
a, b = input().split()
turns[int(a)] = b
d = 0
dx = [0,1,0,-1]
dy = [1,0,-1,0]
x, y = 0, 0
body = [[x, y]]
time = 0
while True:
# move forward
x += dx[d]
y += dy[d]
# finish check
if [x,y] in body:
break
if x<0 or x>=n or y<0 or y>=n: # touch wall
break
body.append([x,y])
# apple check
if apples[1000*x+y]==1:
apples[1000*x+y] = 0
else:
body.pop(0)
time += 1
if time in turns:
if turns[time] == "D": # turn right
d += 1
d = d % 4
else:
d += 3
d = d % 4
print(time+1)
전형적인 구현 문제다!
댓글