기본 콘텐츠로 건너뛰기

백준알고리즘 1149번, RGB거리

RGB 색상별로 가중치가 주어질 때, 가장 값이 낮게 집을 칠하는 문제입니다.

전략 : 특별한 전략은 필요 없네요.

-----------------------------------------------------------------------------

#include <stdio.h>
#include <string.h>

using namespace std;

int dp[1000][3];

int main(void) {
memset(dp, -1, 0);
int n;
int t;
int min = 100000;

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif

scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d %d %d", &dp[i][0], &dp[i][1], &dp[i][2]);
}

for (int i = 0; i < t; i++) {
dp[i + 1][0] = dp[i+1][0] + (dp[i][1] < dp[i][2] ? dp[i][1] : dp[i][2]);
dp[i + 1][1] = dp[i+1][1] + (dp[i][0] < dp[i][2] ? dp[i][0] : dp[i][2]);
dp[i + 1][2] = dp[i+1][2] + (dp[i][0] < dp[i][1] ? dp[i][0] : dp[i][1]);
}

for (int i = 0; i < 3; i++) {
if (dp[t - 1][i] < min)
min = dp[t - 1][i];
}

printf("%d\n", min);

#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif

return 0;
}

댓글

이 블로그의 인기 게시물

프로그래머스 - 시저 암호

public class Caesar { String caesar ( String s , int n ) { n %= 26 ; String result = "" ; char [] temp = s . toCharArray (); for ( int i = 0 ; i < temp . length ; i ++) { if ( temp [ i ]>= 'a' && temp [ i ]<= 'z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'z' ? ( temp [ i ]+ n - 1 )% 'z' + 'a' : ( temp [ i ]+ n )); else if ( temp [ i ]>= 'A' && temp [ i ]<= 'Z' ) temp [ i ] = ( char ) (( temp [ i ]+ n )> 'Z' ? ( temp [ i ]+ n - 1 )% 'Z' + 'A' : ( temp [ i ]+ n )); result += temp [ i ]; } return result ; } public static void main ( String [] args ) { Caesar c = new Caesar (); System . out . println ( "s는 'a B z', n은 4인 경우: " + c . caesar ( ...