forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_path_sum.cpp
63 lines (52 loc) · 1.48 KB
/
min_path_sum.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
49
50
51
52
53
54
55
56
57
58
/*
Given a n x m grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
*/
#include<bits/stdc++.h>
using namespace std;
int dp[202][202];
int minPathSum(vector<vector<int>>& grid) {
int n=grid.size();
int m=grid[0].size();
int dp[n][m];
for(int i=n-1;i>=0;i--){
for(int j=m-1;j>=0;j--){
if(i==n-1 && j==m-1){
dp[i][j]=grid[i][j];
}else if(i==n-1){
dp[i][j]=grid[i][j]+dp[i][j+1];
}
else if(j==m-1){
dp[i][j]=grid[i][j]+dp[i+1][j];
}
else{
dp[i][j]=grid[i][j]+min(dp[i+1][j],dp[i][j+1]);
}
}
}
return dp[0][0];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--)
{
int rows,cols;
cin>>rows>>cols;
vector<vector<int>> grid( rows , vector<int> (cols));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
cin>> grid[i][j];
}
}
cout << minPathSum(grid) << "\n";
}
return 0;
}