Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준
- 자료구조
- ㅣ풀이
- 게리멘더링2
- 스택의 특징
- 백준 1158
- 시간 복잡도
- AVL 시간 복잡도
- 5397
- 백준 17779
- 풀이
- 백준 17822
- 별 찍기 10
- 백준 2447
- 1764
- 해시구현
- heap
- qorwns
- 구현
- dfs
- 해시 구현
- 조세퍼스 순열
- c#
- 백준 1406
- C/C++ 구현
- 버킷 정렬
- 원판 돌리기
- 백준 5397
- 백준 17471
- Stack 이란
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1149] R, G, B거리 본문
분류 : DP
요구사항
이웃해 있는 집이 같은 색깔으로 중복되지 않게 최소비용으로 집 색깔 선택하기
풀이
n번째 집을 선택했을때 가격이 최소이려면
6가지 경우는 고려하여야 한다
현재 집 + 이웃집까지의 최소(같은 색상X)
if(color==1)
ref = min(map[num][color] + func(num-1,color+2),map[num][color] + func(num-1,color+1));
else if(color==2)
ref = min(map[num][color] + func(num-1,color-1),map[num][color] + func(num-1,color+1));
else if(color==3)
ref = min(map[num][color] + func(num-1,color-1),map[num][color] + func(num-1,color-2));
색깔 별로 모든 경우는 고려해주고 최소를 출력한다
#include <iostream>
#include <string.h>
#include <queue>
//#include "windows.h"
#include <algorithm>
#include <list>
using namespace std;
int d[1010][6];
int n;
int map[1010][6];
int func(int num, int color){
if(num==0) return 0;
if(num==1){
if(color==1) return map[1][1];
else if(color==2) return map[1][2];
else if(color==3) return map[1][3];
}
int &ref = d[num][color];
if(ref!=-1){
return d[num][color];
}
if(color==1)
ref = min(map[num][color] + func(num-1,color+2),map[num][color] + func(num-1,color+1));
else if(color==2)
ref = min(map[num][color] + func(num-1,color-1),map[num][color] + func(num-1,color+1));
else if(color==3)
ref = min(map[num][color] + func(num-1,color-1),map[num][color] + func(num-1,color-2));
return ref;
}
int main(){
scanf("%d",&n);
for(int i=1 ; i <= n ; i++)
//빨 초 파
for(int j=1 ; j<=3; j++)
{ scanf("%d",&map[i][j]);
d[i][j]=-1;
}
int ans=987654321;
ans=min(func(n,1) , min(func(n,2) , func(n,3)));
cout<<ans<<endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 1305] 광고 (0) | 2020.03.28 |
---|---|
[백준 1786] 찾기 (0) | 2020.03.28 |
[백준 11726] 2n 타일링 (0) | 2020.03.26 |
[백준 9095] 1, 2, 3 더하기 (0) | 2020.03.26 |
[백준 18809] Gaaaaaaaaaarden (0) | 2020.03.26 |
Comments