-
Notifications
You must be signed in to change notification settings - Fork 0
/
2644_촌수계산.kt
40 lines (33 loc) · 899 Bytes
/
2644_촌수계산.kt
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
package self_boj
import java.util.*
private fun main() {
var ans = -1
val n = readLine()!!.toInt()
val (a, b) = readLine()!!.split(" ").map { it.toInt() }
val m = readLine()!!.toInt()
val arr = Array(n + 1) { mutableListOf<Int>() }
val visited = BooleanArray(n + 1) { false }
visited[0] = true
repeat(m) {
val cur = readLine()!!.split(" ").map { it.toInt() }
arr[cur[0]].add(cur[1])
arr[cur[1]].add(cur[0])
}
val q = LinkedList<Pair<Int, Int>>()
q.offer(Pair(a, 0))
visited[a] = true
while(q.isNotEmpty()) {
val now = q.poll()
if(now.first == b) {
ans = now.second
break
}
arr[now.first].forEach {
if(!visited[it]) {
q.offer(Pair(it, now.second + 1))
visited[it] = true
}
}
}
print(ans)
}