PE 081, finished by simple dynamic program

This commit is contained in:
2022-05-08 00:15:18 +02:00
parent dd43fcc0c5
commit 4fdb2238d9
2 changed files with 141 additions and 0 deletions

61
projecteuler/081/main.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include <bits/stdc++.h>
using namespace std;
int main(){
cout << "Hello this is Patrick" << endl;
auto start = chrono::high_resolution_clock::now();
// I imagine we have to do something with memoization, building up the cheapest path from
// the bottom right corner up to the top left corner
// First read the matrix file
vector<vector<long long>> matrix(80);
ifstream matrixFile("matrix.txt");
for(int i = 0; i < 80; ++i){
for(int j = 0; j < 80; ++j){
int n;
matrixFile >> n;
matrix[i].push_back(n);
if(j != 79){
char c;
matrixFile >> c;
}
}
}
vector<vector<long long>> dp(80);
for(int i = 0; i < 80; ++i){
for(int j = 0; j < 80; ++j){
dp[i].push_back(-1);
}
}
dp[79][79] = matrix[79][79];
for(int i = 78; i >= 0; --i){
dp[i][79] = matrix[i][79] + dp[i+1][79];
}
for(int j = 78; j >= 0; --j){
dp[79][j] = matrix[79][j] + dp[79][j+1];
}
for(int i = 78; i >= 0; --i){
for(int j = 78; j >= 0; --j){
dp[i][j] = matrix[i][j] + min(dp[i+1][j], dp[i][j+1]);
}
}
cout << dp[0][0] << endl;
auto duration = chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start);
cout << (float)duration.count()/1000 << endl;
return 0;
}