File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
_WeeklyChallenges/W03-[IMPLEMENTATION] Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ '''
2+ BOJ #3190. 뱀 (골드5)
3+ https://www.acmicpc.net/problem/3190
4+ 유형: 구현, 시뮬레이션, 자료구조
5+ '''
6+
7+
8+ import sys
9+ from collections import deque
10+ input = sys .stdin .readline
11+ n = int (input ())
12+ k = int (input ())
13+ maps = [[0 ] * (n + 1 ) for _ in range (n + 1 )]
14+ for _ in range (k ):#사과의 위치
15+ x ,y = map (int ,input ().split ())
16+ maps [x ][y ] = 2
17+ info = {}
18+ l = int (input ())
19+ for _ in range (l ):# 뱀의 방향변환정보 (초, 방향 L:왼쪽 D:오른쪽)
20+ sec , direct = input ().split ()
21+ info [int (sec )] = direct
22+ time = 0
23+ dx = [1 ,0 ,- 1 ,0 ]
24+ dy = [0 ,1 ,0 ,- 1 ]
25+ x , y = 1 , 1
26+ maps [y ][x ] = 1
27+ d = 0
28+ snakes = deque ([(1 , 1 )])
29+
30+ while True :
31+ nx , ny = x + dx [d ], y + dy [d ]
32+ # 뱀의 몸통에 닿거나 벽에 부딪히는 경우 종료
33+ if nx <= 0 or ny <= 0 or nx > n or ny > n or (nx ,ny ) in snakes :
34+ break
35+ # 사과를 먹지 못하면 꼬리 없애기
36+ if maps [ny ][nx ]!= 2 :
37+ a ,b = snakes .popleft ()
38+ maps [b ][a ]= 0
39+ x , y = nx , ny
40+ maps [y ][x ] = 1
41+ snakes .append ((nx , ny ))
42+ time += 1
43+
44+ # 시간에 해당하는 방향전환 정보가 있을 경우
45+ if time in info .keys ():
46+ if info [time ] == "D" :
47+ d = (d + 1 )% 4
48+ else :
49+ nd = 3 if d == 0 else d - 1
50+ d = nd
51+ print (time + 1 )
You can’t perform that action at this time.
0 commit comments