• Home
  • About
    • Moon photo

      Moon

      개발자는 자고 싶다.

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

백준 - 1012 유기농 배추

05 Jun 2022

Reading time ~1 minute

문제

1012 유기농 배추

screencapture

답

kotlin code

fun main() {
   repeat(readln().toInt()) {
      val (y, x, cabbageCount) = readln().split(" ").map { it.toInt() }
      val field = Array(y) { BooleanArray(x) }
      repeat(cabbageCount) {
         readln().split(" ").map { it.toInt() }.let { field[it[0]][it[1]] = true }
      }
      var resultCount = 0
      for ((yIndex, row) in field.withIndex()) {
         for ((xIndex, column) in row.withIndex()) {
            if (!column) continue
            resultCount++
            paint(xIndex, yIndex, field)
         }
      }
      println(resultCount)
   }
}

fun paint(x:Int, y:Int, field: Array<BooleanArray>) {
   if (!field[y][x]) return
   field[y][x] = false
   if (x < field[y].lastIndex) paint(x + 1, y, field)
   if (x > 0) paint(x - 1, y, field)
   if (y < field.lastIndex) paint(x, y + 1, field)
   if (y > 0) paint(x, y - 1, field)
}


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