[BOJ] 1149 RGB 거리

Time Lapse :None

1149.cpp

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

#define MAXSIZE 1001
int cost[3][MAXSIZE];
int cache[3][MAXSIZE];
int n;

int dp(int color,int depth)
{
int &ret = cache[color][depth];
if (ret != -1)
return ret;

if (depth == n - 1)
{
ret = cost[color][depth];
return ret;
}

ret = min(dp((color + 1) % 3, depth + 1), dp((color + 2) % 3, depth + 1)) + cost[color][depth];
return ret;
}

int main(void)
{
cin >> n;
memset(cache, -1, sizeof(cache));

for (int i = 0; i < n; i++)
for (int j = 0; j < 3; j++)
cin >> cost[j][i];

int answer = min(dp(0, 0), dp(1, 0));
answer = min(answer, dp(2, 0));

cout << answer << endl;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1149/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.