-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmaximumpath.cpp
48 lines (40 loc) · 921 Bytes
/
maximumpath.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
47
48
// Project Euler 2019
// Vinicius Rodrigues 17.10.2019
// Maximum Path Sum I : Problem 18
// R: 1074
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int size = 15;
int main()
{
vector<vector<int>> matrix;
string cmd;
// input
for(int i = 0; i < 15; i++)
{
matrix.push_back(vector<int>());
for(int j = 0; j <= i; j++)
{
cin >> cmd;
(--matrix.end()) -> push_back(atoi(cmd.c_str()));
}
}
// varredura somando o maximo das duas celulas anteriores
for(int i = 1; i < matrix.size(); i++)
{
for(int j = 0; j < matrix[i].size(); j++)
{
if(j != i and j > 0)
matrix[i][j] += max(matrix[i-1][j], matrix[i-1][j-1]);
else
matrix[i][j] += j == i ? matrix[i-1][j-1] : matrix[i-1][j];
}
}
auto triBase = *(--matrix.end());
printf("%d\n", *max_element(triBase.begin(), triBase.end()));
}