• Home
  • About
    • back
    • Moon photo

      Moon

      개발자는 자고 싶다.

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

백준 - 1018 체스판 다시 칠하기

05 Jun 2022

Reading time ~1 minute

문제

1018 체스판 다시 칠하기

screencapture

답

kotlin code

fun main() {
    val rowCount = readln().split(" ").first().toInt()
    val inputs = Array(rowCount) { readln() }
    val patternPair = Pair("WBWBWBWB", "BWBWBWBW")
    var min = 32
    for(y in 0..(inputs.size-8)) {
        for(x in 0..(inputs[y].length-8)) {
            repeat(2) {
                var total = 0
                for (yy in (y..y + 7)) {
                    val pattern = if ((yy + it) % 2 == 0) patternPair.first else patternPair.second
                    total += inputs[yy].substring(x, x + 8).getDiffCount(pattern)
                    if (total > min) break
                }
                min = kotlin.math.min(total, min)
            }
        }
    }
    println(min)
}

fun String.getDiffCount(other:String):Int {
    var count = 0
    for(i in (0..kotlin.math.min(this.lastIndex, other.lastIndex))) {
        if(this[i] != other[i]) count++
    }
    return count
}


baekjoonkotlin코틀린백준실버브루트포스 알고리즘 Share Tweet +1