https://www.acmicpc.net/problem/1697
1697번: 숨바꼭질
수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. 만약, 수빈이의 위치가 X일
www.acmicpc.net
import Foundation
var demessions = readLine()!
.split(separator: " ")
.compactMap { Int($0) }
let NotVisited = -1
let start = demessions.first!
let end = demessions.last!
var array = Array(repeating: NotVisited, count: 100001)
var queue: [Int] = [start]
array[start] = 0
while array[end] == NotVisited {
let current = queue.removeFirst()
for next in [current+1, current-1, current*2] {
if !(0...100000 ~= next) { continue }
if array[next] != NotVisited { continue }
queue.append(next)
array[next] = array[current] + 1
}
}
print(array[end])
'백준 > 실버' 카테고리의 다른 글
[Swift] 7672번 : 나이트의 이동 (0) | 2024.03.18 |
---|---|
[Swift] 1012번 : 유기농 배추 (0) | 2024.03.18 |
[Swift] 2178번 : 미로탐색 (0) | 2024.02.05 |
[Swift] 1926번 : 그림 (0) | 2024.02.05 |
[Swift] 10773번 : 제로 (0) | 2024.01.12 |