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

kldaji·2022년 3월 21일
1

백준

목록 보기
41/76
post-custom-banner

문제링크

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

profile
다양한 관점에서 다양한 방법으로 문제 해결을 지향하는 안드로이드 개발자 입니다.
post-custom-banner

0개의 댓글