package yukicoder fun main() { val (h, w) = readLine()!!.split(" ").map(String::toInt) val blacks = (0 until h).flatMap { h2 -> readLine()!! .mapIndexed { index, c -> Pair(index, c) } .filter { it.second != '.' } .map { Pair(h2, it.first) } } // blacks.forEach { println(it) } // println("----------") if (blacks.size % 2 != 0) { println("NO") return } val redSize = blacks.size / 2 fun canPaint(vararg redIndexes: Int): Boolean { return when (redIndexes.size >= redSize) { true -> { val redMasus = redIndexes.map { blacks[it] } val blueMasus = blacks.filter { !redMasus.contains(it) } for (i in 0 until h) { for (j in 0 until w) { if (i == 0 && j == 0) { continue; } val movedReds = redMasus.map { Pair(it.first + i, it.second + j) } if (movedReds == blueMasus) { return true } } } return false } false -> (redIndexes.last() until redSize).map { canPaint(*redIndexes, it) }.any() } } when (canPaint(1)) { true -> println("YES") false -> println("NO") } }