๐Ÿ’œ๊ฐœ๋ฐœ๊ณผ ์ผ์ƒ (โ‰ง∇โ‰ฆ)๏พ‰

[๋ฐฑ์ค€ c++] 1149 RGB๊ฑฐ๋ฆฌ (์‹ค1) ๋ณธ๋ฌธ

CS/๋ฐฑ์ค€

[๋ฐฑ์ค€ c++] 1149 RGB๊ฑฐ๋ฆฌ (์‹ค1)

๊ฐ•์˜์„œ 2023. 9. 25. 15:55

๐Ÿ“Œ ๋ฌธ์ œ

RGB๊ฑฐ๋ฆฌ์—๋Š” ์ง‘์ด N๊ฐœ ์žˆ๋‹ค. ๊ฑฐ๋ฆฌ๋Š” ์„ ๋ถ„์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ N๋ฒˆ ์ง‘์ด ์ˆœ์„œ๋Œ€๋กœ ์žˆ๋‹ค.

์ง‘์€ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘ ์ค‘ ํ•˜๋‚˜์˜ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

  • 1๋ฒˆ ์ง‘์˜ ์ƒ‰์€ 2๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • N๋ฒˆ ์ง‘์˜ ์ƒ‰์€ N-1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • i(2 ≤ i ≤ N-1)๋ฒˆ ์ง‘์˜ ์ƒ‰์€ i-1๋ฒˆ, i+1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

์ฒ˜์Œ์— ์ด ๋ฌธ์ œ๋ฅผ DFS๋กœ ํ’€์—ˆ๋‹ค. ํ’€๋ฆฐ๋‹ค... ํ•˜์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ... ์™œ์ผ๊นŒ ๊ณ ๋ฏผํ•ด๋ดค๋‹ค

#include <iostream>

using namespace std;

int MIN = 999000;
int N;
int Red[1001];
int Green[1001];
int Blue[1001];

//DP๋กœ ์ด์ „ ๊ฐ’ ์ €์žฅ
int total[1001][3];

//DFS search
void Search(int Before, int idx){
    
    //๋์ผ ๊ฒฝ์šฐ
    if(idx == N){
        if(MIN > total[idx -1][Before]) MIN = total[idx -1][Before];
        return;
    }
    
    //์•ž์ด Red์ผ ๊ฒฝ์šฐ
    if(Before == 0){
        
        //Green ์ƒ‰์น 
        total[idx][1] = total[idx-1][Before] + Green[idx];
        Search(1, idx + 1);
        
        //Blue ์ƒ‰์น 
        total[idx][2] = total[idx-1][Before] + Blue[idx];
        Search(2, idx + 1);

    }
    //์•ž์ด Green์ผ ๊ฒฝ์šฐ
    else if(Before == 1){
        
        //Red ์ƒ‰์น 
        total[idx][0] = total[idx-1][Before] + Red[idx];
        Search(0, idx + 1);
                
        //Blue ์ƒ‰์น 
        total[idx][2] = total[idx-1][Before] + Blue[idx];
        Search(2, idx + 1);
    }
    //์•ž์ด Blue์ผ ๊ฒฝ์šฐ
    else if(Before == 2){
        
        //Red ์ƒ‰์น 
        total[idx][0] = total[idx-1][Before] + Red[idx];
        Search(0, idx + 1);
        
        //Green ์ƒ‰์น 
        total[idx][1] = total[idx-1][Before] + Green[idx];
        Search(1, idx + 1);
        
    }
}

int main()
{
    cin >> N;
    for(int i = 0; i < N; i++) cin >> Red[i] >> Green[i] >> Blue[i];
    
    total[0][0] = Red[0];
    total[0][1] = Green[0];
    total[0][2] = Blue[0];
    //search 
    Search(0,1);
    Search(1,1);
    Search(2,1);
    
    //output
    cout << MIN << endl;

    return 0;
}

๋ฌธ์ œ์˜ ์ฝ”๋“œ์ธ๋ฐ DFS ์ฝ”๋“œ๊ฐ€ ์ตœ์ ํ™”๊ฐ€ ์•ˆ ๋˜์–ด ์žˆ๋Š”๊ฐ€? ๊ณ„์† ์ˆ˜์ •ํ•ด๋ดค์ง€๋งŒ ์•„๋ฌด๋ฆฌ ์ƒ๊ฐํ•ด๋„ ์•Œ ์ˆ˜ ์—†๋‹ค ์œผ์•…!

 

But, ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

์ฒซ๋ฒˆ์งธ ์ง‘์€ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ์ง‘์€ 3๊ฐœ๊ณ  ๊ทธ ์ดํ›„ 2~N๊นŒ์ง€์˜ ์ง‘์€ ์ด 2๊ฐœ์˜ ์„ ํƒ์ง€๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ,

์‹œ๊ฐ„๋ณต์žก๋„๋Š” 3 X 2^(N-1) => O(2^N)...

๋น„ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ž„์„ ๊นจ๋‹ฌ์•˜๋‹ค... ์ด๋Ÿฐใ… 

 

์ด ๋ฌธ์ œ๋Š” DP๋กœ ํ’€ ์ˆ˜ ์žˆ๋‹ค.. DP๋กœ ์ด์ „์˜ ์ตœ์†Œ๊ฐ’์„ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ๊ณ„์† ์ €์žฅํ•ด๋‚˜๊ฐ€๋Š” ์ž‘์—…์„ ๋ฐ˜๋ณตํ•˜๋ฉด

N๋ฐ”ํ€ด๋งŒ ํƒ์ƒ‰์„ ํ•˜๋ฉด๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.. ์ด๋Ÿด์ˆ˜๊ฐ€...

์ด๋ž˜์„œ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋ฏธ๋ฆฌ ์ƒ๊ฐํ•ด๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค

 

๐Ÿ’ป ์ •๋‹ต ์ฝ”๋“œ

#include <iostream>
using namespace std;

int MIN;
int N;
int Red[1001];
int Green[1001];
int Blue[1001];

//DP๋กœ ์ด์ „ ๊ฐ’ ์ €์žฅ
int total[1001][3];

int main()
{
    cin >> N;
    for(int i = 0; i < N; i++) cin >> Red[i] >> Green[i] >> Blue[i];
    
    total[0][0] = Red[0];
    total[0][1] = Green[0];
    total[0][2] = Blue[0];
    //search 
    
    
    for(int i = 1; i < N; i++){
        
        total[i][0] = total[i-1][1] < total[i-1][2] ? total[i-1][1] : total[i-1][2];
        total[i][0] += Red[i];
        
        total[i][1] = total[i-1][0] < total[i-1][2] ? total[i-1][0] : total[i-1][2];
        total[i][1] += Green[i];
        
        total[i][2] = total[i-1][0] < total[i-1][1] ? total[i-1][0] : total[i-1][1];
        total[i][2] += Blue[i];
    }

    MIN = total[N-1][0] < total[N-1][1] ? total[N-1][0] : total[N-1][1];
    MIN = total[N-1][2] < MIN ? total[N-1][2] : MIN;
    
    //output
    cout << MIN << endl;

    return 0;
}