project-euler

https://projecteuler.net/
Log | Files | Refs | README

Euler_18.cpp (648B)


      1 #include <fstream>
      2 
      3 #include "Euler.h"
      4 
      5 int Euler::MaximumPathSum()
      6 {
      7     std::ifstream fin;
      8     std::vector<std::string> str_rows;
      9 
     10     fin.open("files/p067_triangle.txt");
     11 
     12     std::string temp;
     13     while(std::getline(fin, temp))
     14         str_rows.push_back(temp);
     15 
     16     fin.close();
     17 
     18     std::vector<std::vector<int>> rows;
     19 
     20     for (std::string s :str_rows)
     21         rows.push_back(EulerUtility::tokenizer(s, ' '));
     22 
     23     for (int i = rows.size() - 2; i >= 0; --i)
     24         for (unsigned j = 0; j < rows[i].size(); ++j)
     25             rows[i][j] += (rows[i + 1][j] > rows[i + 1][j + 1]) ? rows[i + 1][j] : rows[i + 1][j + 1];
     26 
     27     return rows[0][0];
     28 }