티스토리 뷰
문제 링크
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
삼각형 가장 위쪽의 위치에서 최대경로는
1. 현재위치에서 한칸 아래 위치
2. 현재위치에서 오른쪽 한칸 아래 위치
두 가지 위치의 최대 경로 중 큰값에 현재 위치의 값을 더해주면 됩니다.
구현 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <cstring> | |
#define endl '\n' | |
#define fastio cin.sync_with_stdio(false); cin.tie(nullptr) | |
using namespace std; | |
const int MAX = 101; | |
int arr[MAX][MAX]; | |
int d[MAX][MAX]; | |
int n; | |
int dp(int x, int y){ | |
if (y == n) | |
return arr[y][x]; | |
if (d[y][x] != -1) | |
return d[y][x]; | |
return d[y][x] = arr[y][x] + max(dp(x, y+1), dp(x+1, y+1)); | |
} | |
int main(){ | |
fastio; | |
int _; | |
cin >> _; | |
while (_--){ | |
memset(d, -1, sizeof(d)); | |
cin >> n; | |
for (int i=1;i<=n;i++) | |
for (int j=1;j<=i;j++) | |
cin >> arr[i][j]; | |
cout << dp(1, 1) << endl; | |
} | |
return 0; | |
} |
'Coding Test > 알고스팟' 카테고리의 다른 글
[C++] 알고스팟/동적계획법 - 합친 LIS (0) | 2020.08.19 |
---|---|
[C++] 알고스팟/동적계획법 - Longest Increasing Sequence (0) | 2020.08.18 |
[C++] 알고스팟/동적계획법 - 와일드카드 (0) | 2020.08.16 |
[C++] 알고스팟/동적계획법 - 외발 뛰기 (0) | 2020.08.15 |
[C++] 알고스팟/분할정복 - 팬미팅 (0) | 2020.08.09 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 합친 lis
- 외발 뛰기
- 배열과 문자열
- import
- Hadoop
- pyspark
- 완전탐색
- hive
- HDFS
- 백준
- Django
- 알고스팟
- 두니발 박사의 탈옥
- 하둡
- Sqoop
- 삼각형 위의 최대 경로
- 종만북
- 출전 순서 정하기
- 코딩인터뷰 완전분석
- 삼각형 위의 최대 경로 수 세기
- 팰린드롬 구하기
- microwaving lunch boxes
- 분할정복
- python
- 2225
- 하이브
- C++
- HiveQL
- Jaeha's Safe
- 스파크
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함