#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <queue>
#include <vector>
using namespace std;
void Print(const vector<vector<int>> &grid) {
cout << "Printing grid:" << endl;
for (const auto &row : grid) {
for (int col : row) {
cout << col << " ";
}
cout << endl;
}
}
class Solution {
public:
int MinPathSum(vector<vector<int>> &grid) {
const int rows = grid.size();
if (!rows) {
return -1;
}
const int cols = grid[0].size();
if (!cols) {
return -1;
}
auto dp = vector<vector<int>>(rows, vector<int>(cols));
dp[0][0] = grid[0][0];
for (int row = 1; row < rows; ++row) {
dp[row][0] = dp[row - 1][0] + grid[row][0];
}
for (int col = 1; col < cols; ++col) {
dp[0][col] = dp[0][col - 1] + grid[0][col];
}
for (int row = 1; row < rows; ++row) {
for (int col = 1; col < cols; ++col) {
dp[row][col] = min(dp[row - 1][col], dp[row][col - 1]) + grid[row][col];
}
}
return dp[rows - 1][cols - 1];
}
};
int main() {
vector<vector<int>> intervals = {{1, 3, 1}, {1, 5, 1}, {4, 2, 1}};
Solution solution;
Print(intervals);
const int result = solution.MinPathSum(intervals);
cout << "Result: " << result << endl;
vector<vector<int>> grid2 = {{1, 2, 3}, {4, 5, 6}};
Print(grid2);
const int result2 = solution.MinPathSum(grid2);
cout << "Result2: " << result2 << endl;
return 0;
}