-
algospot - trianglepathAlgorithm/알고스팟 2020. 2. 2. 14:45123456789101112131415161718192021222324252627282930313233343536373839404142//algospot - trianglepath#include <iostream>#include <vector>using namespace std;int N;vector<vector<int>> tri;vector<vector<int>> cache;void init(){cin >> N;tri = vector<vector<int>>(N, vector<int>(N));cache = vector<vector<int>>(N, vector<int>(N, -1));for(int i = 0; i<N; i++)for(int j = 0; j<=i; j++)cin >> tri[i][j];}int dp(int x, int y){if(x == N-1)return tri[x][y];int& ret = cache[x][y];if(ret != -1) return ret;ret = dp(x+1, y);ret = max(ret, dp(x+1, y+1));ret += tri[x][y];return ret;}int main(){int C; cin >> C;for(int tn = 0; tn < C; ++tn){init();cout << dp(0, 0) << "\n";}}
cs https://algospot.com/judge/problem/read/TRIANGLEPATH
algospot.com :: TRIANGLEPATH
삼각형 위의 최대 경로 문제 정보 문제 6 1 2 3 7 4 9 4 1 7 2 7 5 9 4 위 형태와 같이 삼각형 모양으로 배치된 자연수들이 있습니다. 맨 위의 숫자에서 시작해, 한 번에 한 칸씩 아래로 내려가 맨 아래 줄로 내려가는 경로를 만들려고 합니다. 경로는 아래 줄로 내려갈 때마다 바로 아래 숫자, 혹은 오른쪽 아래 숫자로 내려갈 수 있습니다. 이 때 모든 경로 중 포함된 숫자의 최대 합을 찾는 프로그램을 작성하세요. 입력 입력의 첫 줄에는 테
algospot.com
DP문제
'Algorithm > 알고스팟' 카테고리의 다른 글
algospot - jlis (0) 2020.02.02 algospot - lis (0) 2020.02.02 algospot - wildcard (0) 2020.02.02 algospot - jumpgame (0) 2020.02.02 algospot - lan (0) 2020.01.31