* Computer Science/Algorithm

baekjun 1923. 정수 삼각형

soicem 2018. 8. 1. 14:21

 동적계획법 기본 문제


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
#include <iostream>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
int triangle[500][500];
int cache[501][501];
int N;
int getMaxPath(int y, int x) {
    if (y == N)
        return 0;
    int & ret = cache[y][x];
    if (ret != -1)
        return ret;
    return ret = max(getMaxPath(y + 1, x), getMaxPath(y + 1, x + 1)) + triangle[y][x];
}
 
int main() {
 
    cin >> N;
    memset(cache, -1sizeof(cache));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= i; j++) {
            cin >> triangle[i][j];
        }
    }
    cout << getMaxPath(00<< endl;
    system("pause");
    return 0;
}
cs


'* Computer Science > Algorithm' 카테고리의 다른 글

baekjun 1463. 1로 만들기  (0) 2018.08.02
baekjun 2579. 계단 오르기  (0) 2018.08.02
baekjun 1149. RGB거리  (0) 2018.08.01
baekjoon 1003. 피보나치 함수  (0) 2018.07.18
CCI moderate, hard  (0) 2017.12.14