1+ """
2+ condition
3+ 1. NxN map , some apple , N+1 walls ,
4+ 2. snack 's time : size = 0 : 1 , right way
5+
6+ Rule
7+ 1.먼저 뱀은 몸길이를 늘려 머리를 다음칸에 위치시킨다.
8+ 2.만약 벽,자기자신의 몸과 부딪히면 게임이 끝난다. =game over 조건
9+ <사과 - 몸길이 변화 >
10+ 3.만약 이동한 칸에 사과 O => 그 칸에 있던 사과가 없어지고 꼬리 stay , 머리 길어짐 => 즉 몸길이 변함
11+ 4.만약 이동한 칸에 사과 X => 몸길이를 줄여서 꼬리가 위치한 칸을 비워준다(즉, 담칸으로 이동만 함). 즉, 몸길이는 변하지 않는다.
12+ -> 머리 부터 ,그리고 꼬리 이동(사과 유무)
13+ #Variable
14+ 1. time , 2. 뱀이 차지하는 영역 : stack , 3.
15+
16+ Flow
17+ 1. 입력을 통해 map, 이동 반경
18+
19+
20+ """
21+ import sys
22+
23+ ''' 1. 입력 값 변수에 할당(field구축 , 뱀의 방향 정보)
24+ - field : N+2
25+ 빈 곳 = 0
26+ 벽, 뱀의 위치 = -1
27+ 사과 = 1
28+ '''
29+
30+ N = int (sys .stdin .readline ()) # 보드 크기 => N+2*(q)
31+ K = int (sys .stdin .readline ()) # 사과 개수
32+
33+ field = [[0 for _ in range (N + 2 )] for k in range (N + 2 )]
34+ # 뱀의 첫 위치 & 벽
35+ field [1 ][1 ] = - 1
36+ for i in range (len (field )):
37+ for j in range (len (field [0 ])) :
38+ if i in [0 , N + 1 ] or j in [0 ,N + 1 ] :
39+ field [i ][j ] = - 1
40+
41+ # 사과 위치
42+ for k in range (K ) :
43+ i , j = map (int , sys .stdin .readline ().split ())
44+ field [i ][j ] = 1
45+
46+ # 뱀의 방향 정보 -> rotate_time=[time, direction] # 방향을 이동하기 전에 확인하여 t+1 로 저장
47+
48+ L = int (sys .stdin .readline ())
49+ rotate_time = []
50+ for l in range (L ):
51+ t , d = sys .stdin .readline ().split ()
52+ rotate_time .append ([int (t )+ 1 ,d ])
53+
54+
55+ """
56+ 2. 게임 start
57+ - 뱀의 현재 차지하고 있는 위치 : snack = [[머리 좌표] , [몸통 좌표들],...,[꼬리 좌표]]
58+ - & 차지하는 field = -1
59+ (1) 뱀이 도착한 위치의 field 번호 = -1(본인,벽) 이 아닐때 까지 이동
60+ """
61+ def change_int_direction (time , current_direction ) : # 방향 변환
62+ direction_map = ['r' ,'d' ,'l' ,'t' ] * 2
63+ # direction_dict = {'r,':[0,1] ,'d' :[-1,0] ,'l':[0,-1] , 't':[1,0] }
64+ for ch_t , ch_d in rotate_time :
65+ if time == ch_t : # {'r,':[0,1] ,'d' :[-1,0] ,'l':[0,-1] , 't':[1,0] }
66+ # print(f"change direction ")
67+ idx = direction_map .index (current_direction )
68+ if ch_d == "D" : #right
69+ current_direction = direction_map [idx + 1 ]
70+ elif ch_d == "L" :#left
71+ current_direction = direction_map [idx - 1 ]
72+ break
73+
74+ # direction = direction_dict[current_direction] # [0,1]
75+ return current_direction # [0,1]
76+ from collections import deque
77+ time = 0
78+ snack = deque () # idx =0 머리 , -1 꼬리
79+ snack_direction = 'r' # r,d,l, u
80+ snack .append ([1 ,1 ])
81+ direction_dict = {'r' :[0 ,1 ] ,'d' :[1 ,0 ] ,'l' :[0 ,- 1 ] , 't' :[- 1 ,0 ] }
82+ while (1 ) :
83+ time += 1
84+ # print(f"##time : {time}")
85+ # 사과 확인
86+ head_x , head_y = snack [0 ]
87+ #1. head 이동 - snack 영역 추가
88+ snack_direction = change_int_direction (time , snack_direction )
89+ move_x , move_y = direction_dict [snack_direction ]
90+ head_x += move_x ; head_y += move_y
91+ # print(f"- snack_direction : {snack_direction} # {move_x},{move_y}")
92+ # print(f"new head : {head_x} , {head_y}")
93+ #2.현재 head 위치하는 곳의 조건 확인
94+ if field [head_x ][head_y ] == - 1 : #game over
95+ # print(f"!!!gameover : ")
96+ # print(field)
97+ # print(f"{head_x} , {head_y} = {field[head_x][head_y]}")
98+ break
99+ elif field [head_x ][head_y ] == 0 : # safe(빈곳)
100+ tail_x , tail_y = snack .pop () #W 꼬리 이동
101+ field [tail_x ][tail_y ] = 0
102+ # field[head_x][head_y] == 0,1 (사과, 빈곳 )공통점 - 머리 이동 ,
103+ field [head_x ][head_y ] = - 1 #field 값 변경(뱀이 차지함) - 사과 먹음
104+ snack .appendleft ([head_x ,head_y ]) # head 추가
105+ # print(snack)
106+ print (time )
0 commit comments