문제
2667 단지번호붙이기
답
kotlin code
fun main() {
val xyCount = readln().toInt()
val square = Array(xyCount) { readln().toCharArray().map { if(it == '1') -1 else it.digitToInt() }.toIntArray() }
var count = 0
val sums = mutableListOf<Int>()
for((y, row) in square.withIndex()) {
for((x, column) in row.withIndex()) {
if(column != -1) continue
sums.add(paint((++count), y, x, square))
}
}
println(count)
sums.sorted().forEach { println(it) }
}
fun paint(to: Int, y: Int, x: Int, square: Array<IntArray>): Int = when {
square[y][x] != -1 -> 0
else -> {
square[y][x] = to
var count = 1
count += if (x + 1 < square[y].size) paint(to, y, x + 1, square) else 0
count += if (x > 0) paint(to, y, x - 1, square) else 0
count += if (y + 1 < square.size) paint(to, y + 1, x, square) else 0
count += if (y > 0) paint(to, y - 1, x, square) else 0
count
}
}