문제

1012 유기농 배추

1012 유기농 배추

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

0개의 댓글