File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44
55### 성능 요약
66
7- 메모리: 10616 KB, 시간: 24 ms
7+ 메모리: 74488 KB, 시간: 36 ms
88
99### 분류
1010
11- 너비 우선 탐색 , 그래프 이론, 그래프 탐색
11+ 그래프 이론 , 그래프 탐색, 너비 우선 탐색, 역추적
1212
1313### 제출 일자
1414
15- 2024년 1월 13일 05:38:27
15+ 2025년 8월 27일 23:40:04
1616
1717### 문제 설명
1818
Original file line number Diff line number Diff line change 1+ let nk = readLine ( ) !. split { $0 == " " } . map { Int ( String ( $0) ) ! } ,
2+ n = nk [ 0 ] ,
3+ k = nk [ 1 ]
4+
5+ var answer = " "
6+ var history = [ Int] ( repeating: - 1 , count: 200002 )
7+ var isVisited = [ Bool] ( repeating: false , count: 200002 )
8+
9+ func bfs( ) {
10+ var queue = [ Int] ( )
11+ var index = 0
12+ queue. append ( n)
13+ isVisited [ n] = true
14+
15+ while queue. count > index {
16+ let currentPoint = queue [ index]
17+
18+ if currentPoint == k {
19+ var shortestHistory = [ k]
20+ var i = k
21+ while i != n {
22+ let prev = history [ i]
23+ shortestHistory. append ( prev)
24+ i = prev
25+ }
26+ answer. write ( " \( shortestHistory. count- 1 ) \n " )
27+ answer. write ( shortestHistory. reversed ( ) . map { String ( $0) } . joined ( separator: " " ) )
28+ return
29+ }
30+
31+ let teleport = currentPoint * 2
32+ if ( 0 ... 100000 ) . contains ( teleport) , !isVisited[ teleport] {
33+ queue. append ( teleport)
34+ isVisited [ teleport] = true
35+ history [ teleport] = currentPoint
36+ }
37+
38+ let go = currentPoint + 1
39+ if ( 0 ... 100000 ) . contains ( go) , !isVisited[ go] {
40+ queue. append ( go)
41+ isVisited [ go] = true
42+ history [ go] = currentPoint
43+ }
44+
45+ let back = currentPoint - 1
46+ if ( 0 ... 100000 ) . contains ( back) , !isVisited[ back] {
47+ queue. append ( back)
48+ isVisited [ back] = true
49+ history [ back] = currentPoint
50+ }
51+ index += 1
52+ }
53+
54+ return
55+ }
56+
57+ bfs ( )
58+ print ( answer)
You can’t perform that action at this time.
0 commit comments