Home 백준 - 1012 유기농 배추
Post
Cancel

백준 - 1012 유기농 배추

문제

1012 유기농 배추

screencapture

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() {
   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)
}
This post is licensed under CC BY 4.0 by the author.

백준 - 4153 직각삼각형

백준 - 7568 덩치