-
Notifications
You must be signed in to change notification settings - Fork 3
/
숨바꼭질.swift
48 lines (38 loc) · 1.36 KB
/
숨바꼭질.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Foundation
let NK = readLine()!.split(separator: " ").map { Int(String($0))! }
func solution(N: Int, K: Int) -> Int {
var queue: [Int] = [N]
var index = 0
var minTimes: [Int?] = Array(repeating: nil, count: 100001)
minTimes[N] = 0
while queue.count > index {
defer{ index += 1 }
let currentLocation = queue[index]
let lowerPosition = currentLocation - 1
let upperPosition = currentLocation + 1
let doubledPosition = currentLocation * 2
if 0...100000 ~= lowerPosition {
if minTimes[lowerPosition] == nil {
minTimes[lowerPosition] = minTimes[currentLocation]! + 1
queue.append(lowerPosition)
}
}
if 0...100000 ~= upperPosition {
if minTimes[upperPosition] == nil {
minTimes[upperPosition] = minTimes[currentLocation]! + 1
queue.append(upperPosition)
}
}
if 0...100000 ~= doubledPosition {
if minTimes[doubledPosition] == nil {
minTimes[doubledPosition] = minTimes[currentLocation]! + 1
queue.append(doubledPosition)
}
}
if let result = minTimes[K] {
return result
}
}
return -1
}
print(solution(N: NK[0], K: NK[1]))