๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 1149 RGB ๊ฑฐ๋ฆฌ ์ž๋ฐ” ๋ฌธ์ œ ํ’€์ด

by Wonit 2021. 1. 25.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 1149๋ฒˆ RGB ๊ฑฐ๋ฆฌ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

ํ•ด๊ฒฐ๋ฒ•

๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•œ ์ „์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋ณด์ž.


์šฐ์„  ์ด ๋ฌธ์ œ๋Š” ์ž…๋ ฅ์ด n์ด๋ผ๋ฉด arr[n][3] ์˜ ๋ฐฐ์—ด์ด ํ•„์š”ํ•˜๋‹ค.


์™œ? n๊ฐœ์˜ ์ง‘์ด ์žˆ์„ ๋•Œ ๊ฐ๊ฐ์ด ๊ฐ–๋Š” RGB ์ƒ‰์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—.

 

๊ทธ๋ž˜์„œ ์˜ˆ์ œ์˜ ์ž…๋ ฅ์„ ์šฐ๋ฆฌ๊ฐ€ ์ƒ๊ฐํ•œ ๋ฐฐ์—ด์— ๋‹ด์œผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

3
26 40 83
49 60 57
13 89 99

    26          40          83
house[0][0] house[0][1] house[0][2]

    49          60          57
house[1][0] house[1][1] house[1][2]

    13          89          99
house[2][0] house[2][1] house[2][2]

์ด์ œ ์ „์ฒ˜๋ฆฌ๊ฐ€ ๋๋‚ฌ๋‹ค.


๋ฌธ์ œ๋ฅผ ์ ‘๊ทผํ•ด๋ณด์ž.

 

์ ‘๊ทผ๋ฒ•

 

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ house[1][1]์˜ ์ง‘์€ ์ ˆ๋Œ€๋กœ house[0][1]์˜ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์„ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.


ํ•œ ์ง‘์„ ์„ ํƒํ–ˆ์„ ๋•Œ, ์ „ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฒน์น˜์ง€๋งŒ ์•Š๋Š”๋‹ค๋ฉด 2๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด๋‹ค.


๊ทธ๋Ÿผ ๋ชจ๋“  ์ƒ‰์„ ์น ํ•  ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์จฐ ์ง‘์„ ์ œ์™ธํ•˜๊ณ  ์ƒˆ๋กญ๊ฒŒ house ๋ฐฐ์—ด์„ updateํ•  ์ˆ˜ ์žˆ๋‹ค.


์ง‘์˜ ์ˆ˜(i)์™€ ์ƒ‰์˜ ๊ฐ€์ค‘์น˜(j : RGB ์ด 3์ด ๋œ๋‹ค) ๊ฐ€ ๋‹ด๊ธด i, j๋งŒํผ ๋ฐ˜๋ณต์„ ๋Œ๋ฉด์„œ ํ˜„์žฌ ๊นŒ์ง€์˜ ๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ์žก๊ณผ ํ˜„์žฌ ์ง‘์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๋”ํ•˜๋ฉด ํ•ด๋‹น ์ง‘์˜ ์ตœ์†Œ ๊ฐ€์ค‘์น˜์˜ ๋น„์šฉ์„ ์•Œ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?

3
26 40 83
49 60 57
13 89 99

      26              40               83
  house[0][0]     house[0][1]     house[0][2]

49 + min(40, 83)  60 + min(26, 83)  57 + min(26, 40)
  house[1][0]      house[1][1]      house[1][2]

13 + min(house[1][1], house[1][2]) ...
  house[2][0]     house[2][1]     house[2][2]

๊ทธ๋ ‡๋‹ค.

 

์ •๋‹ต์ด๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— 3๊ฐ€์ค‘์น˜ ํ•ฉ์ด ์ตœ์†Œ์ธ ์ง‘์„ ์„ ํƒํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ •๋‹ต ์ฝ”๋“œ

import java.io.*;
public class Main {

    static int[][] rgb;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int n = Integer.parseInt(br.readLine());
        rgb = new int[n][3];
        for (int i = 0; i < n; i++) {
            String[] rgbs = br.readLine().split(" ");
            for (int j = 0; j < rgbs.length; j++) {
                rgb[i][j] = Integer.parseInt(rgbs[j]);
            }
        }
        bw.flush();
        bw.close();
    }
    private static int rgbWeight(int n, int m){
        if(n == 0) return myMinimum(rgb[0][0], rgb[0][1], rgb[0][2]);

        return myMinimum(rgb[n-1][0], rgb[n-1][1], rgb[n-1][2]);
    }

    private static int myMinimum(int a, int b, int c){
        return Math.min(a, Math.min(b, c));
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€