Skip to content

Commit 6f57f9b

Browse files
committed
[Gold IV] Title: 숨바꼭질 4, Time: 36 ms, Memory: 74488 KB -BaekjoonHub
1 parent 07087ab commit 6f57f9b

2 files changed

Lines changed: 61 additions & 3 deletions

File tree

백준/Gold/13913. 숨바꼭질 4/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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)

0 commit comments

Comments
 (0)