File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change 1+ n = int (input ())
2+ k = int (input ())
3+ data = [[0 ] * (n + 1 ) for _ in range (n + 1 )] # 맵 정보
4+ info = [] # 방향 회전 정보
5+
6+ # 맵 정보 (사과 있는 곳은 1로 표시)
7+ for _ in range (k ):
8+ a , b = map (int , input ().split ())
9+ data [a ][b ] = 1
10+
11+ # 방향 회전 정보 입력
12+ l = int (input ())
13+ for _ in range (l ):
14+ x , c = input ().split ()
15+ info .append ((int (x ), c ))
16+
17+ # 처음에는 오른쪽을 보고 있으므로 (동, 남, 서, 북)
18+ dx = [0 , 1 , 0 , - 1 ]
19+ dy = [1 , 0 , - 1 , 0 ]
20+
21+ def turn (direction , c ):
22+ if c == "L" :
23+ direction = (direction - 1 ) % 4
24+ else :
25+ direction = (direction + 1 ) % 4
26+ return direction
27+
28+ def simulate ():
29+ x , y = 1 , 1 # 뱀의 머리 위치
30+ data [x ][y ] = 2 # 뱀이 존재하는 위치는 2로 표시
31+ direction = 0 # 처음에는 동쪽을 보고 있음
32+ time = 0 # 시작한 뒤에 지난 '초' 시간
33+ index = 0 # 다음에 회전할 정보
34+ q = [(x , y )] # 뱀이 차지하고 있는 위치 정보 (꼬리가 앞쪽)
35+
36+ while True :
37+ nx = x + dx [direction ]
38+ ny = y + dy [direction ]
39+ # 맵 범위 안에 있고, 뱀의 몸통이 없는 위치라면
40+ if 1 <= nx and nx <= n and 1 <= ny and ny <= n and data [nx ][ny ] != 2 :
41+ # 사과가 없다면 이동 후에 꼬리 제거
42+ if data [nx ][ny ] == 0 :
43+ data [nx ][ny ] = 2
44+ q .append ((nx , ny ))
45+ px , py = q .pop (0 )
46+ data [px ][py ] = 0
47+ # 사과가 있다면 이동 후에 꼬리 그대로 두기
48+ if data [nx ][ny ] == 1 :
49+ data [nx ][ny ] = 2
50+ q .append ((nx , ny ))
51+ # 벽이나 뱀의 몸통과 부딪혔다면
52+ else :
53+ time += 1
54+ break
55+ x , y = nx , ny # 다음 위치로 머리를 이동
56+ time += 1
57+ if index < l and time == info [index ][0 ]: # 회전할 시간인 경우 회전
58+ direction = turn (direction , info [index ][1 ])
59+ index += 1
60+ return time
61+
62+ print (simulate ())
You can’t perform that action at this time.
0 commit comments