IT/알고리즘
[백준] RGB 거리
남갯
2024. 11. 25. 23:06
728x90
SMALL
https://www.acmicpc.net/problem/1149
fun main() = with(Scanner(System.`in`)) {
val size = nextInt()
val count = Array<Int>(size * 3) { 0 }
for (i in count.indices) {
count[i] = nextInt()
}
val dp = Array<Int>(count.size) { Int.MAX_VALUE }
for (i in 0..2) {
dp[i] = count[i]
}
for (i in 0 until size - 1) {
for (j in 0..2) {
if (j == 0) {
dp[(i + 1) * 3 + 1] =
min(dp[(i + 1) * 3 + 1], dp[(i) * 3] + count[(i + 1) * 3 + 1])
dp[(i + 1) * 3 + 2] =
min(dp[(i + 1) * 3 + 2], dp[(i) * 3] + count[(i + 1) * 3 + 2])
}
if (j == 1) {
dp[(i + 1) * 3 + 0] =
min(dp[(i + 1) * 3 + 0], dp[(i) * 3 + 1] + count[(i + 1) * 3 + 0])
dp[(i + 1) * 3 + 2] =
min(dp[(i + 1) * 3 + 2], dp[(i) * 3 + 1] + count[(i + 1) * 3 + 2])
}
if (j == 2) {
dp[(i + 1) * 3 + 0] = min(dp[(i + 1) * 3 + 0], dp[(i) * 3 + 2] + count[(i + 1) * 3 + 0])
dp[(i + 1) * 3 + 1] = min(dp[(i + 1) * 3 + 1], dp[(i) * 3 + 2] + count[(i + 1) * 3 + 1])
}
}
}
println(dp.takeLast(3).min())
}
728x90
LIST