-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path14496번.swift
40 lines (38 loc) · 1.19 KB
/
14496번.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
// 출처 : 백준 그대, 그머가 되어
// https://www.acmicpc.net/problem/14496
// 풀이 : hogumachu
let firstInput = readLine()!.split(separator: " ").map{Int(String($0))!}
let secondInput = readLine()!.split(separator: " ").map{Int(String($0))!}
let a = firstInput[0], b = firstInput[1]
let n = secondInput[0], m = secondInput[1]
var graph: [[(Int, Int)]] = Array(repeating: [], count: n + 1)
var visited: [Bool] = Array(repeating: false, count: n + 1)
var weights: [Int] = Array(repeating: 999_999_999, count: n + 1)
for _ in 0..<m {
let thirdInput = readLine()!.split(separator: " ").map{Int(String($0))!}
graph[thirdInput[0]].append((thirdInput[1], 1))
graph[thirdInput[1]].append((thirdInput[0], 1))
}
weights[a] = 0
visit(a)
print(weights[b] == 999_999_999 ? "-1" : weights[b])
func visit(_ now: Int) -> Void {
if now == b {
return
}
visited[now] = true
for i in graph[now] {
weights[i.0] = min(weights[i.0], weights[now] + 1)
}
let next = weights.enumerated()
.filter({
!visited[$0.offset]
})
.min(by:{
$0.element < $1.element
})?
.offset
if next != nil {
visit(next!)
}
}