백준 - 1018 체스판 다시 칠하기
문제
1018 체스판 다시 칠하기
답
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
28
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
}
This post is licensed under CC BY 4.0 by the author.