fun main(args: Array) { val (H, W) = readLine()!!.split(" ").map(String::toInt) // 入力を各セルごとの二次元リストに val cellsList: List> = List(H) { readLine()!!.chunked(1) } // 行ごとに、黒色セルのインデックスのリストを取得 val blackIndices = cellsList.map { it.mapIndexedNotNull { index, value -> if (value == "#") index else null } } /* //Check Print cellsList.forEach { println(it) } blackIndices.forEach { println(it) } */ for (vecY in (-H + 1)..(H - 1)) for (vecX in (-W + 1)..(W - 1)) { if (tryPaintB(cellsList, blackIndices, H, W, vecY, vecX)) { println("YES") return } } println("NO") } fun tryPaintB( cellsList: List>, blackIndices: List>, H: Int, W: Int, vecY: Int, vecX: Int ): Boolean { //val cells = cellsList.map { it.toMutableList() } val rangeY = if (vecY < 0) ((H - 1) downTo 0).toList() else (0 until H).toList() val indices = if (vecX < 0) blackIndices.map { it.reversed().toMutableList() } else blackIndices.map { it.toMutableList() } var flg: Boolean? = null /* //Check Print println("vec = $vecY, $vecX") println("Y_range = $rangeY") println("blackIndices =") indices.forEach { println(it) } */ for (y in rangeY) { while (indices[y].isNotEmpty()) { var trgY = y var trgX = indices[y].first() while (rangeY.contains(trgY) && indices[trgY].contains(trgX)) { //cells[y][x] = "R" indices[trgY].removeAt(0) trgY += vecY trgX += vecX if (rangeY.contains(trgY) && indices[trgY].contains(trgX)) { //cells[trgY][trgX] = "B" indices[trgY].remove(trgX) if (flg == null) flg = true trgY += vecY trgX += vecX } else { //flg = false return false } } // if (rangeY.contains(trgY) && indices[trgY].contains(trgX)) { // //cells[trgY][trgX] = "B" // indices[trgY].remove(trgX) // if (flg == null) flg = true // } else { // //flg = false // return false // } } } /* //Check Print cells.forEach { println(it.joinToString("")) } println(flg) println(" ") */ return flg ?: false }