package net.ipipip0129.kotlin.yukicoder import kotlin.math.sqrt fun checkCorner(w: Int, h: Int, sizeW: Int, sizeH: Int): Boolean { val checkPos = arrayOf(arrayOf(0, 0), arrayOf(0, sizeW + 1), arrayOf(sizeH + 1, 0), arrayOf(sizeH + 1, sizeW + 1)) checkPos.forEach { if (it[0] == h && it[1] == w) { return true } } return false } fun Int.square(): Int = this * this fun main(args: Array) { val sizeSplit = readLine()!!.split(" ") val sizeH = sizeSplit[0].toInt() val sizeW = sizeSplit[1].toInt() val tailMap = Array(sizeH + 2, { Array(sizeW + 2, { 0 }) }) val checkRange = { index: Int, size: Int -> index == 0 || index == size + 1 } tailMap.forEachIndexed { indexH, ints -> ints.forEachIndexed { indexW, _ -> if (checkRange(indexW, sizeW) || checkRange(indexH, sizeH)) { if (!checkCorner(indexW, indexH, sizeW, sizeH)) { tailMap[indexH][indexW] = 2 } } } } for (i in 0 until sizeH) { readLine()!!.toCharArray().forEachIndexed { index, c -> tailMap[i + 1][index + 1] = c.toString().toInt() } } val blackPos = mutableListOf>() val standPos = mutableListOf>() tailMap.forEachIndexed { indexH, ints -> ints.forEachIndexed { indexW, i -> when (i) { 1 -> blackPos.add(arrayOf(indexH, indexW)) 2 -> standPos.add(arrayOf(indexH, indexW)) } } } val timeList = mutableListOf() standPos.forEach { pos -> var time = 0.0 blackPos.forEach { time += sqrt(((pos[0] - it[0]).square() + (pos[1] - it[1]).square()).toDouble()) } timeList.add(time) } println(timeList.min()) }