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
| import java.util.*
fun main() = with(Scanner(System.`in`)) { val (n, k) = nextInt() to nextInt() val line = Array(100001) { -1 } val queue: Queue<Int> = LinkedList()
line[n] = 0 queue.offer(n)
while (queue.isNotEmpty()) { val current = queue.poll() val dx = intArrayOf(1, -1, current)
for (dir in dx.indices) { val next = current + dx[dir]
if (next < 0 || next > 100000) continue if (line[next] != -1) continue
line[next] = line[current] + 1 queue.offer(next) } }
print(line[k]) }
|