๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] RGB ๊ฑฐ๋ฆฌ 2 BOJ #17404

by seolhee2750 2022. 10. 2.
๋ฌธ์ œ

https://www.acmicpc.net/problem/17404

 

17404๋ฒˆ: RGB๊ฑฐ๋ฆฌ 2

์ฒซ์งธ ์ค„์— ์ง‘์˜ ์ˆ˜ N(2 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	
	static int n;
	static int[][] r, g, b;
	static int answer = Integer.MAX_VALUE;
	
	public static void first(int red, int green, int blue) {
		for(int i = 0; i < 3; i++) {
			r[0][i] = Integer.MAX_VALUE - 1000;
			g[0][i] = Integer.MAX_VALUE - 1000;
			b[0][i] = Integer.MAX_VALUE - 1000;
		}
		
		r[0][0] = red;
		g[0][1] = green;
		b[0][2] = blue;
	}
	
	public static void middle(int i, int red, int green, int blue) {
		r[i][0] = red + Math.min(r[i-1][1], r[i-1][2]);
		r[i][1] = green + Math.min(r[i-1][0], r[i-1][2]);
		r[i][2] = blue + Math.min(r[i-1][0], r[i-1][1]);
		
		g[i][0] = red + Math.min(g[i-1][1], g[i-1][2]);
		g[i][1] = green + Math.min(g[i-1][0], g[i-1][2]);
		g[i][2] = blue + Math.min(g[i-1][0], g[i-1][1]);
		
		b[i][0] = red + Math.min(b[i-1][1], b[i-1][2]);
		b[i][1] = green + Math.min(b[i-1][0], b[i-1][2]);
		b[i][2] = blue + Math.min(b[i-1][0], b[i-1][1]);
		
		if(i == n-1) findAnswer();
	}
	
	public static void findAnswer() {		
		answer = Math.min(answer, Math.min(r[n-1][1], r[n-1][2]));
		answer = Math.min(answer, Math.min(g[n-1][0], g[n-1][2]));
		answer = Math.min(answer, Math.min(b[n-1][0], b[n-1][1]));
		
		System.out.println(answer);
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		n = Integer.parseInt(in.readLine());
		r = new int[n][3];
		g = new int[n][3];
		b = new int[n][3];
		
		for(int i = 0; i < n; i++) {
			st = new StringTokenizer(in.readLine());
			int red = Integer.parseInt(st.nextToken());
			int green = Integer.parseInt(st.nextToken());
			int blue = Integer.parseInt(st.nextToken());
			
			if(i == 0) { // ์ฒซ ๋ฒˆ์งธ ์ง‘์„ ๊ฐ๊ฐ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ํ”ฝ์Šคํ•ด์„œ ์ƒ๊ฐํ•ด์ฃผ๊ธฐ
				first(red, green, blue);
			} else { // ๋‘ ๋ฒˆ์งธ ~ ๋งˆ์ง€๋ง‰ ์ง‘
				middle(i, red, green, blue);
			}
		}
	}
}

๐Ÿ‘‰ DP ๋ฌธ์ œ
์ด ๋ฌธ์ œ๋Š” RGB ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์™€ ๊ฑฐ์˜ ๋™์ผํ•œ๋ฐ, ํ•œ ๊ฐ€์ง€ ์ฐจ์ด์ ์ด ์žˆ๋‹ค๋ฉด,

์ง‘์„ ์‚ฌ์ดํด์ฒ˜๋Ÿผ ์ƒ๊ฐํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์ง‘๊ณผ ๋งˆ์ง€๋ง‰ ์ง‘๋„ ๊ฐ™์€ ์ƒ‰์ด ๋˜์ง€ ๋ชปํ•˜๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ!

 

๋”ฐ๋ผ์„œ r, g, b 2์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ ,

๊ฐ๊ฐ ์ฒซ ๋ฒˆ์งธ ์ง‘์ด ๋นจ๊ฐ„์ƒ‰, ์ดˆ๋ก์ƒ‰, ํŒŒ๋ž€์ƒ‰์ธ ๊ฒฝ์šฐ๋กœ ํ”ฝ์Šคํ•ด์ฃผ๊ณ  ๋ฐฐ์—ด์„ ์ฑ„์›Œ๋‚˜๊ฐ”๋‹ค.

 

๋‘ ๋ฒˆ์งธ ์ง‘ ๋ถ€ํ„ฐ๋Š” RGB ๊ฑฐ๋ฆฌ ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ,

r, g, b ๋ฐฐ์—ด์ด ๊ฐ๊ฐ ํ˜„์žฌ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๊ฐ€์ •ํ•˜๊ณ 

์ด์ „ ์ง‘์˜ ๊ฐ€๋Šฅํ•œ ์ƒ‰ ์ค‘ ๊ฐ€์žฅ ์ตœ์†Œ ๋น„์šฉ์ด ๋ฐœ์ƒํ•˜๋Š” ์ƒ‰์„ ์„ ํƒํ•˜๋Š” ์‹์œผ๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ

์ฒซ ๋ฒˆ์งธ ์ง‘์˜ ํ”ฝ์Šค ๋œ ์ƒ‰์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ƒ‰์˜ ์ž๋ฆฌ๋Š” ๋ชจ๋‘ Integer.MaxValue๋กœ ์ฑ„์› ๋‹ค.

(-1000์„ ํ•ด์ค€ ์ด์œ ๋Š”, ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ตœ๋Œ€ ๋น„์šฉ์ด 1000 ์ด๋ฏ€๋กœ

MaxValue ๊ฐ’์— ์ƒˆ๋กœ์šด ๋น„์šฉ์„ ๋”ํ•˜๋”๋ผ๋„ ๊ฐ’์ด ๋„˜์–ด๊ฐ€์„œ ์Œ์ˆ˜๊ฐ€ ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ๊ธฐ ์œ„ํ•จ)

 

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰ ์ง‘๊นŒ์ง€ ๋ชจ๋‘ ์น ํ•˜๋ฉด,

r, g, b ๊ฐ๊ฐ ์ฒซ ๋ฒˆ์งธ ์ง‘์„ ์น ํ•œ ์ƒ‰์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ์ƒ‰ ์ค‘ ์ตœ์†Œ ๊ฐ’์„ ๊ณจ๋ผ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ–ˆ๋‹ค.

 

โœ”๏ธ ์•„๋ž˜ ์˜ˆ์ œ์˜ ๊ฒฐ๊ณผ ๋„์ถœ ๊ณผ์ •์„ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ–ˆ๋‹ค.

3
26 40 83
49 60 57
13 89 99

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • RGB ๊ฑฐ๋ฆฌ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๊ธฐ ๋•Œ๋ฌธ์— ์‰ฝ๊ฒŒ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‚˜ ํšจ์œจ์ ์œผ๋กœ ํ‘ผ๊ฑด ์•„๋‹Œ๊ฒƒ๊ฐ™์•„์„œ,!!! ๋‹ค๋ฅธ ํ’€์ด๋“ค์„ ๋ณด๊ณ  ๊ณต๋ถ€ ํ•ด๋ด์•ผ๊ฒ ๋‹น.

๋Œ“๊ธ€