- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path5.py
62 lines (55 loc) · 1.95 KB
/
5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
n=int(input())
k=int(input())
data= [[0] * (n+1) for_inrange(n+1)] # 맵 정보
info= [] # 방향 회전 정보
# 맵 정보(사과 있는 곳은 1로 표시)
for_inrange(k):
a, b=map(int, input().split())
data[a][b] =1
# 방향 회전 정보 입력
l=int(input())
for_inrange(l):
x, c=input().split()
info.append((int(x), c))
# 처음에는 오른쪽을 보고 있으므로(동, 남, 서, 북)
dx= [0, 1, 0, -1]
dy= [1, 0, -1, 0]
defturn(direction, c):
ifc=="L":
direction= (direction-1) %4
else:
direction= (direction+1) %4
returndirection
defsimulate():
x, y=1, 1# 뱀의 머리 위치
data[x][y] =2# 뱀이 존재하는 위치는 2로 표시
direction=0# 처음에는 동쪽을 보고 있음
time=0# 시작한 뒤에 지난 '초' 시간
index=0# 다음에 회전할 정보
q= [(x, y)] # 뱀이 차지하고 있는 위치 정보(꼬리가 앞쪽)
whileTrue:
nx=x+dx[direction]
ny=y+dy[direction]
# 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
if1<=nxandnx<=nand1<=nyandny<=nanddata[nx][ny] !=2:
# 사과가 없다면 이동 후에 꼬리 제거
ifdata[nx][ny] ==0:
data[nx][ny] =2
q.append((nx, ny))
px, py=q.pop(0)
data[px][py] =0
# 사과가 있다면 이동 후에 꼬리 그대로 두기
ifdata[nx][ny] ==1:
data[nx][ny] =2
q.append((nx, ny))
# 벽이나 뱀의 몸통과 부딪혔다면
else:
time+=1
break
x, y=nx, ny# 다음 위치로 머리를 이동
time+=1
ifindex<landtime==info[index][0]: # 회전할 시간인 경우 회전
direction=turn(direction, info[index][1])
index+=1
returntime
print(simulate())