Home 백준 - 2667 단지번호붙이기
Post
Cancel

백준 - 2667 단지번호붙이기

문제

2667 단지번호붙이기

screencaptures

kotlin code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
    }
}
This post is licensed under CC BY 4.0 by the author.

백준 - 3009 네 번째 점

백준 - 2231 분해합