Skip to content

Commit b402060

Browse files
committed
add: 백준/RGB거리
1 parent 24b2afa commit b402060

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

boj/1149(DP).js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// RGB거리
2+
// https://www.acmicpc.net/problem/1149
3+
const fs = require('fs');
4+
const stdin = (process.platform === 'linux'
5+
? fs.readFileSync('/dev/stdin').toString().trim()
6+
: `3
7+
26 40 83
8+
49 60 57
9+
13 89 99`
10+
).split('\n');
11+
12+
const input = (() => {
13+
let line = 0;
14+
return () => stdin[line++];
15+
})();
16+
17+
const N = parseInt(input());
18+
19+
const inputColors = [];
20+
21+
for (let i = 0; i < N; i++) {
22+
const rgbEach = input()
23+
.split(' ')
24+
.map((each) => parseInt(each));
25+
inputColors.push(rgbEach);
26+
}
27+
28+
const colorSum = Array.from(Array(N), () => Array(3).fill(0));
29+
30+
inputColors.forEach((each, index) => {
31+
if (index === 0) {
32+
colorSum[index][0] = each[0];
33+
colorSum[index][1] = each[1];
34+
colorSum[index][2] = each[2];
35+
} else {
36+
colorSum[index][0] =
37+
each[0] + Math.min(colorSum[index - 1][1], colorSum[index - 1][2]);
38+
colorSum[index][1] =
39+
each[1] + Math.min(colorSum[index - 1][0], colorSum[index - 1][2]);
40+
colorSum[index][2] =
41+
each[2] + Math.min(colorSum[index - 1][0], colorSum[index - 1][1]);
42+
}
43+
});
44+
45+
console.log(Math.min(...colorSum[N-1]));
46+
47+
/*
48+
49+
## How
50+
이번주에 단비같이 주어진 실버 문제였다. DP의 기초적인 문제였는데 RGB 색상의 합이 최소가 되도록 선택하여 비용을 더해야 하는데 이전 집의 색을 배제해야 한다.
51+
52+
이에대한 점화식을 구해서 바텀업으로 문제의 요구사항을 구할 수 있도록 하였다.
53+
54+
## Retrospective
55+
56+
*/

0 commit comments

Comments
 (0)