fork download
  1. import kotlin.math.abs
  2.  
  3. fun main(args: Array<String>) {
  4. if(args.isNotEmpty()) {
  5. System.setIn(java.io.FileInputStream(args[0]))
  6. }
  7. if(args.size >= 2) {
  8. System.setOut(java.io.PrintStream(args[1]))
  9. }
  10.  
  11. val numTests = readLine()!!.toInt()
  12. require(numTests in 1..100)
  13.  
  14. var sumN = 0
  15. repeat(numTests) {
  16. val (N, K) = readLine()!!.split(" ").map{ it.toInt() }
  17. require(N in 1..100)
  18. require(K in 1..N*N)
  19. val H = List(N) { readLine()!!.split(" ").map{ it.toInt() - 1 } }
  20. require(H.flatten().toSet() == (0 until K).toSet())
  21.  
  22. sumN += N
  23. require(sumN <= 100)
  24.  
  25. val pos = List(K) { mutableListOf<Pair<Int,Int>>() }
  26. for(i in 0 until N) for(j in 0 until N) pos[H[i][j]].add(Pair(i, j))
  27.  
  28. val tb = List(N) { IntArray(N) }
  29. for(h in 0 until K-1) {
  30. for((x2, y2) in pos[h+1]) {
  31. var cur = N*N*K
  32. for((x1, y1) in pos[h]) cur = minOf(cur, tb[x1][y1] + abs(x1 - x2) + abs(y1 - y2))
  33. tb[x2][y2] = cur
  34. }
  35. }
  36.  
  37. println(pos[K-1].map{ (i, j) -> tb[i][j] }.min()!!)
  38. }
  39.  
  40. require(readLine() == null)
  41.  
  42. }
Runtime error #stdin #stdout #stderr 0.08s 33960KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" kotlin.KotlinNullPointerException
	at ProgKt.main(prog.kt:11)