[백준 2630 - Kotlin] 색종이 만들기

문제링크

import java.io.BufferedReader
import java.io.BufferedWriter

private lateinit var bufferedReader: BufferedReader
private lateinit var bufferedWriter: BufferedWriter
private lateinit var papers: MutableList<List<Int>>
private lateinit var answer: Array<Int>

fun main() {
    bufferedReader = System.`in`.bufferedReader()
    bufferedWriter = System.out.bufferedWriter()

    // 1. get n
    val n = bufferedReader.readLine().toInt()

    // 2. get papers
    papers = mutableListOf()
    repeat(n) {
        val paper = bufferedReader
            .readLine()
            .split(" ")
            .map { it.toInt() }
        papers.add(paper)
    }

    // 3. check color
    answer = Array(2) { 0 }
    checkColor(n, 0, 0)

    bufferedWriter.write("${answer[0]}\n")
    bufferedWriter.write("${answer[1]}\n")

    bufferedReader.close()
    bufferedWriter.close()
}

fun checkColor(n: Int, startX: Int, startY: Int) {
    val color = papers[startX][startY]

    if (n == 1) {
        answer[color]++
        return
    }

    for (i in startX until startX + n) {
        for (j in startY until startY + n) {
            if (papers[i][j] != color) {
                checkColor(n / 2, startX, startY)
                checkColor(n / 2, startX + n / 2, startY)
                checkColor(n / 2, startX, startY + n / 2)
                checkColor(n / 2, startX + n / 2, startY + n / 2)
                return
            }
        }
    }

    answer[color]++
}

좋은 웹페이지 즐겨찾기