1149๋ฒ: RGB๊ฑฐ๋ฆฌ
์ฒซ์งธ ์ค์ ์ง์ ์ N(2 ≤ N ≤ 1,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์๋ ๊ฐ ์ง์ ๋นจ๊ฐ, ์ด๋ก, ํ๋์ผ๋ก ์น ํ๋ ๋น์ฉ์ด 1๋ฒ ์ง๋ถํฐ ํ ์ค์ ํ๋์ฉ ์ฃผ์ด์ง๋ค. ์ง์ ์น ํ๋ ๋น์ฉ์ 1,000๋ณด๋ค ์๊ฑฐ๋
www.acmicpc.net
์๊ณ ๋ฆฌ์ฆ [์ ๊ทผ ๋ฐฉ๋ฒ]
ํ์ด์กฐ๊ฑด์ ์ค๋ช ํ์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ผ๋จ ๊ฐ๊ฐ์ ์ง์ ์์น ํด์ผํ๊ณ , ์์ ์ข ๋ฅ๋ 3๊ฐ์ง๊ฐ ์ฃผ์ด์ง๋ค ( ๋นจ๊ฐ, ์ด๋ก, ํ๋ )
๊ทธ๋ฆฌ๊ณ ๊ฐ ์ง๋ง๋ค ํด๋น ์์ ์น ํ๋ ๋น์ฉ์ด ์ฃผ์ด์ง๋ค. ๊ทธ๋ฆฌ๊ณ ์ ์ฒด ์ง์ ์์น ํ ๋ ์ต์ ๋น์ฉ์ ๊ตฌํ๋ ๊ฒ์ด๋ค.
์ด๋ ์ค์ํ ์ ์ด๋ผ๋ฉด ์ธ์ ํ ์ง๋ผ๋ฆฌ๋ ์์ด ๊ฒน์น๋ฉด ์๋๋ค๋ ์ ์ด๋ค.
์๋ฅผ๋ค์ด 3๋ฒ ์ง์ ์ด๋ก์์ ์น ํ๋ค๋ฉด, 2๋ฒ์ง๊ณผ 4๋ฒ์ง์ ์ด๋ก์์ ์น ํ ์ ์๋ค.
์ด๋ฌํ ์กฐ๊ฑด์ผ๋ก ์ธํด ๋น์ฉ์ ๊ตฌํ ๋ ์ต์๊ฐ๋ง์ ์ฐพ์์ ํ๋ฉด ํ๋ฆฌ๊ฒ ๋๋ค.
๋ง์ฝ ๊ฐ ์ง๋ง๋ค ์ต์๊ฐ์ ์ฐพ์ ๋์ ํฉ์ ํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ผ์ด ์๊ธด๋ค.
1๋ฒ ์ง ์ต์๊ฐ=1
์ธ์ ํ ์ง๋ผ๋ฆฌ๋ ๊ฐ์ ์์ ์น ํ ์ ์์ผ๋ฏ๋ก 2๋ฒ์ง์ ์ด๋ก์๊ณผ ํ๋์ ์ค
์ต์๊ฐ์ ์ฐพ์ ๋์ ํฉ. ์ฆ 103+ 1
์ด์ ์ง์ด ์ด๋ก์์ด์์ผ๋ฏ๋ก ๋นจ๊ฐ์ ์ง๊ณผ ํ๋์ ์ง ์ค ์ต์๊ฐ์ ๋์ ํฉ. ์ฆ 100+104
์ฆ, ์์ ๊ฐ์ด ํ๋ฉด ์ถ๋ ฅ์ 204๊ฐ ๋๊ฒ ์ง๋ง ์ด๊ฒ ์ ๋ต์ผ ์ ์๊ฒ ์ง๋ง ๋ต์ ์๋๋ค.
์ฌ์ค ์ ๋ต์ 100(1๋ฒ์ง ์ด๋ก) + 1(2๋ฒ์ง ๋นจ๊ฐ) + 1(3๋ฒ์ง ์ด๋ก)์ธ 102๊ฐ ์ ๋ต์ด๋ค.
๊ฐ ์ง๋ณ ์ต์๊ฐ์ด ์๋ ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ ๋นจ๊ฐ์ ์นธ์ ์ ํํด์ผ ์ฌ๋ฐ๋ฅธ ์ ๋ต์ด ๋๋ค.
๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฐ ์ง์ ์ต์๊ฐ์ ์ฐพ์ ๋์ ํฉ์ ๊ตฌํ๋ ๊ฒ์ด ์๋ ๋ชจ๋ ๊ฒฝ๋ก์ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฐพ์์ ์ต์ข ์ ์ผ๋ก ์์ ๋์ ํฉ์ ์ฐพ์์ผ ํ๋ค.
๊ฐ ์ง์ ๋น์ฉ์ Cost๋ผ๊ณ ํ ๋, ์ด๋ฅผ ํ ๋จ๊ณ์ฉ ์ ์ด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
(์ด๋ min์ ๋ ๊ฐ์ ๊ฐ ์ค ์ต์๊ฐ์ ์๋ฏธํ๋ฉฐ +=์ ๋์ ํฉ์ด๋ค.)
์ฆ, ์ ํ์์ผ๋ก ๋ณธ๋ค๋ฉด N์ ๋ํ์ฌ ์ธ ๊ฐ์ง ์ผ์ด์ค์ ๋ํด ๋ค์๊ณผ ๊ฐ๋ค.
- ๋นจ๊ฐ์ผ ๊ฒฝ์ฐ
Cost[N][0] = min( Cost[N-1][1], Cost[N-1][2] ) + Cost[N][0] - ์ด๋ก์ผ ๊ฒฝ์ฐ
Cost[N][1] = min( Cost[N-1][0], Cost[N-1][2] ) + Cost[N][1] - ํ๋์ผ ๊ฒฝ์ฐ
Cost[N][2] = min( Cost[N-1][0], Cost[N-1][1] ) + Cost[N][2]
์ด์ ๋ถํฐ๋ ๋์ ๊ณํ๋ฒ์ผ๋ก ํ์ดํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ์ด์ ์ ๋ฌธ์ ๋ค(01ํ์ผ, ํผ๋ณด๋์น ๋ฑ)๊ณผ ๊ฐ์ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
๋ค๋ง 3๊ฐ์ง ์ผ์ด์ค๋ฅผ ์ด์ฉํด์ผํ๋ Cost[][] ๋ฐฐ์ด๊ณผ, ํ์ํ๋ฉด์ ๋์ ํฉ์ ์ ์ฅํ DP[][] ํจ์๋ฅผ ๋ง๋ค๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋์ ์ ์ ํ์์ ์ด์ฉํ์ฌ ์ฌ๊ทํจ์๋ก ๊ตฌ์ฑํ ๋ค, ํด๋น ๋ฐฐ์ด์ ์์ง ํ์ํ์ง ์์๋ค๋ฉด ์ฌ๊ท๋ฅผ ํด์ฃผ๊ณ , ๊ทธ ์ธ์ ๊ฒฝ์ฐ๋ DP๋ฐฐ์ด์ ๊ฐ์ ๋ฐํํด์ฃผ๋ฉด ๋๋ค.
๊ฐ ๋น์ฉ์ ์์ฐ์๋ก ์ ๋ ฅ๋๋ค๊ณ ํ๋ DP๋ฐฐ์ด์ -1๋ก ์ด๊ธฐํ ํด์ค ํ์์์ด ํ์ํ์ง ์์ ๊ฒฝ์ฐ๋ฅผ ์ด๊ธฐ๊ฐ(0)์ผ๋ก ์ฌ์ฉํ๋ฉด ๋๋ค.
int[][] Cost = new int[N][3]; // ์ด๋ฏธ ์
๋ ฅ๊ฐ๋ค๋ก ์ด๊ธฐํ๊ฐ ๋์๋ค๊ณ ๊ฐ์
int[][] DP = new int[N][3];
// DP์ ์ฒซ๋ฒ์งธ ๊ฐ(์ง)์ ๊ฐ ์์๋น์ฉ์ ์ฒซ๋ฒ์งธ ๊ฐ์ผ๋ก ์ด๊ธฐํ
DP[0][Red] = Cost[0][Red];
DP[0][Green] = Cost[0][Green];
DP[0][Blue] = Cost[0][Blue];
static int Paint_cost(int N, int color) {
// ๋ง์ฝ ํ์ํ์ง ์์ ๋ฐฐ์ด์ด๋ผ๋ฉด
if(DP[N][color] == 0) {
// color์ ์์ ๋ฐ๋ผ ์ด์ ์ง์ ์๋ก ๋ค๋ฅธ ์์ ์ฌ๊ทํธ์ถํ์ฌ ์ต์๊ฐ๊ณผ ํ์ฌ ์ง์ ๋น์ฉ์ ๋ํด์ DP์ ์ ์ฅํ๋ค
if(color == Red) {
DP[N][Red] = Math.min(Paint_cost(N - 1, Green), Paint_cost(N - 1, Blue)) + Cost[N][Red];
}
else if(color == Green) {
DP[N][Green] = Math.min(Paint_cost(N - 1, Red), Paint_cost(N - 1, Blue)) + Cost[N][Green];
}
else {
DP[N][Blue] = Math.min(Paint_cost(N - 1, Red), Paint_cost(N - 1, Green)) + Cost[N][Blue];
}
}
return DP[N][color];
}
์ด๋ ๊ฒ ์ง๋ฉด ์ดํ ๋ค๋ฅธ ์์ ๋ํ์ฌ ํ์์ ํ ๋ ์ด์ ์ ํ์ํ๋ ๊ฐ์ ๊ฒฝ์ฐ ๋์ด์ ์ฌ๊ทํธ์ถ ํด์ค ํ์ ์์ด ๋ฐ๋ก ํด๋น DP์ ์ธ๋ฑ์ค ๊ฐ์ ์ฌํ์ฉํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ ๋์ ๊ณํ๋ฒ์๋ ์๋ง์ ์๊ณ ๋ฆฌ์ฆ์ผ ๊ฒ์ด๋ค.
์ด๋ ๊ฒ ํ๋์ ํจ์๋ฅผ ๋ง๋ค๊ณ Paint_cost(N, Red), Paint_cost(N, Green), Paint_Cost(N, Blue) ์ ํธ์ถํ์ฌ ํด๋น ๋ฐํ ๊ฐ ์ค ์ต์๊ฐ์ ์ถ๋ ฅํด์ฃผ๋ฉด ๋๋ค.
ํ์ด
- ๋ฐฉ๋ฒ 1 : [์ฌ๊ท + Scanner]
import java.util.Scanner;
public class Main {
final static int Red = 0;
final static int Green = 1;
final static int Blue = 2;
static int[][] Cost;
static int[][] DP;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
Cost = new int[N][3];
DP = new int[N][3];
for(int i = 0; i < N; i++) {
Cost[i][Red] = in.nextInt();
Cost[i][Green] = in.nextInt();
Cost[i][Blue] = in.nextInt();
}
// DP์ ์ฒซ๋ฒ์งธ ๊ฐ(์ง)์ ๊ฐ ์์๋น์ฉ์ ์ฒซ๋ฒ์งธ ๊ฐ์ผ๋ก ์ด๊ธฐํ
DP[0][Red] = Cost[0][Red];
DP[0][Green] = Cost[0][Green];
DP[0][Blue] = Cost[0][Blue];
System.out.print(Math.min(Paint_cost(N- 1, Red), Math.min(Paint_cost(N - 1, Green), Paint_cost(N - 1, Blue))));
}
static int Paint_cost(int N, int color) {
// ๋ง์ฝ ํ์ํ์ง ์์ ๋ฐฐ์ด์ด๋ผ๋ฉด
if(DP[N][color] == 0) {
// color์ ์์ ๋ฐ๋ผ ์ด์ ์ง์ ์๋ก ๋ค๋ฅธ ์์ ์ฌ๊ทํธ์ถํ์ฌ ์ต์๊ฐ๊ณผ ํ์ฌ ์ง์ ๋น์ฉ์ ๋ํด์ DP์ ์ ์ฅํ๋ค
if(color == Red) {
DP[N][Red] = Math.min(Paint_cost(N - 1, Green), Paint_cost(N - 1, Blue)) + Cost[N][Red];
}
else if(color == Green) {
DP[N][Green] = Math.min(Paint_cost(N - 1, Red), Paint_cost(N - 1, Blue)) + Cost[N][Green];
}
else {
DP[N][Blue] = Math.min(Paint_cost(N - 1, Red), Paint_cost(N - 1, Green)) + Cost[N][Blue];
}
}
return DP[N][color];
}
}
- ๋ฐฉ๋ฒ 2 : [๋ฐ๋ณต๋ฌธ + BufferedReader]
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Main {
final static int Red = 0;
final static int Green = 1;
final static int Blue = 2;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] Cost = new int[N][3];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
Cost[i][Red] = Integer.parseInt(st.nextToken());
Cost[i][Green] = Integer.parseInt(st.nextToken());
Cost[i][Blue] = Integer.parseInt(st.nextToken());
}
// 1๋ถํฐ N-1๊น์ง ๊ฐ i๋ณ i-1์ ์๋ก ๋ค๋ฅธ ์์ ์ค ์ต์๊ฐ์ ๋์ ํ์ฌ ๋ํ๋ค.
for (int i = 1; i < N; i++) {
Cost[i][Red] += Math.min(Cost[i - 1][Green], Cost[i - 1][Blue]);
Cost[i][Green] += Math.min(Cost[i - 1][Red], Cost[i - 1][Blue]);
Cost[i][Blue] += Math.min(Cost[i - 1][Red], Cost[i - 1][Green]);
}
System.out.println(Math.min(Math.min(Cost[N - 1][Red], Cost[N - 1][Green]), Cost[N - 1][Blue]));
}
}
๋ชจ๋ ๊ฒฝ์ฐ์ ์ ์ค ์ต์๊ฐ์ ๋ํด๋๊ฐ๋ฉด์ ๊ตฌํ๋ ๋ฐฉ์์ด์ง๋ง ๋์ ๊ณํ๋ฒ์ด๊ธด ํ๋ค.