File tree Expand file tree Collapse file tree 1 file changed +61
-0
lines changed
Expand file tree Collapse file tree 1 file changed +61
-0
lines changed Original file line number Diff line number Diff line change 1+
2+
3+ import sys
4+ from collections import deque
5+
6+ inp = sys .stdin .readline
7+
8+ N , K = map (int , inp ().strip ().split ())
9+ # 해당 위치로 도달한 최소 시간 저장
10+ visited = [- 1 ] * (100_000 + 1 )
11+ visited [N ] = 0
12+
13+ # 최소시간에 해당 위치로 도달한 횟수 저장
14+ ways = [- 1 ] * (100_000 + 1 )
15+ ways [N ] = 1
16+
17+ # (위치, 시간) 형식
18+ queue = deque ()
19+ queue .append ((N ,0 ))
20+
21+ dx = [- 1 , 1 , 2 ]
22+
23+ min_time = - 1
24+
25+ while queue :
26+ subin_loc ,time = queue .popleft ()
27+
28+ # 수빈이가 동생에게 도달한 시간까지만 bfs탐색하고 그 후에 종료
29+ if min_time != - 1 and min_time + 2 == time :
30+ break
31+
32+ # 수빈이가 동생에게 도달한 시간체크
33+ if min_time == - 1 and subin_loc == K :
34+ min_time = visited [subin_loc ]
35+
36+
37+ # 수빈의 위치에서 3가지 이동
38+ for i in range (3 ):
39+ if i == 2 :
40+ new_loc = subin_loc * dx [i ]
41+ else :
42+ new_loc = subin_loc + dx [i ]
43+
44+ # 새 위치가 범위 안
45+ if 0 <= new_loc <= 100_000 :
46+ # 새로 방문한 위치가 이전에 와보지 못했다면,
47+ if visited [new_loc ] == - 1 :
48+ # 방문 시간 설정해주고 경로 초기화
49+ visited [new_loc ] = visited [subin_loc ] + 1
50+ ways [new_loc ] = ways [subin_loc ]
51+ queue .append ((new_loc , time + 1 ))
52+
53+ # 새로 방문한 위치가 이전에 와봤다면, 지금 시간이 이전에 와봤던 시간과 같아야 함.
54+ else :
55+ if visited [new_loc ] == visited [subin_loc ] + 1 :
56+ # 경로 누적시켜줌
57+ ways [new_loc ] = ways [new_loc ] + ways [subin_loc ]
58+
59+
60+ print (visited [K ])
61+ print (ways [K ])
You can’t perform that action at this time.
0 commit comments