• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

    • Learn More
    • Twitter
    • Facebook
    • Instagram
    • Github
    • Steam
  • Posts
    • All Posts
    • All Tags
  • Projects

백준 - 2667 단지번호붙이기

05 Jun 2022

Reading time ~1 minute

문제

2667 단지번호붙이기

screencaptures

답

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
    }
}


baekjoonkotlin코틀린백준실버그래프그래프 이론그래프 탐색깊이 우선 탐색dfs너비 우선 탐색bfs Share Tweet +1