[실패][BOJ 1149] RGB거리

Leehyemin·2023년 3월 12일
0

알고리즘_백준

목록 보기
7/7
#include <iostream> 
using namespace std;

int N =0;
int res[1001][1001]={9999,};
int MIN = 10000;


int func(int n, int c, int B,  int *tri[501]){
     if (n==0){ 
        // 최소 찾음
        if (tri[n][c] < MIN){

            MIN = tri[n][c];
            B=c;
            
        }
    }  
    else{
        if (c != B){ // 이전에 선택햇으면 패스
                //최소 찾음
            if ((res[n-1][B] + tri[n][c]) < MIN){

                MIN= res[n-1][B] + tri[n][c];
                // m=c;
                res[n][c] = MIN;

                // f =  3-(m+b);
                B=c;


            // f= 프론트  b, m이 아닌 RGB값
        }

        }
    }
    cout<< "B: "  <<B<< "값: " << tri[n][B] << " n" <<  n << " c"  << c << " MIN "<< MIN << "  " << endl;
    return MIN;

}

int main(){
    
    cin >> N;

    int** tri = new int*[N+1];
    for (int i = 0; i < N; i++) {
        tri[i] = new int[N+1];  
        fill_n(tri[i], N, 0);
        for (int j = 0; j < 3; j++) { 
            cin >> tri[i][j]; 
        }
    }
    // 합 구하기
    int B=-1;
    int a=-1;
    for (int i = 0; i < N ; i++) {
        MIN = 10000;
        // B=-1;
        
        for (int j = 0; j < 3; j++) { 
            if (j==B){
                continue;
            }
            else{
                B = func(i,j,B, tri);
                // res[i][j] = MIN; 
            }
           
            
        }
        res[i][0] = MIN; 
        res[i][1] = MIN; 
        res[i][2] = MIN; 
        // B=a;

        cout << "min: "<< MIN<< " " << B << endl;


        // b=c;
      
    }
    // // 마지막 노드 중, min 값
    int r_min = 10000;
    for (int j = 0; j < 3; j++) { 
   
        if (r_min > res[N-1][j]) {
            r_min=res[N-1][j];
        }
        cout << res[N-1][j] << endl;

        
    }
    // cout << r_min << endl;

    return 0;
}

0개의 댓글