-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Minimum_Path_Sum.cpp
72 lines (62 loc) · 1.47 KB
/
Minimum_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* ** Given a m x n 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
* **/
/* ** Complexity of Program O(m*n), where m is number of rows and n is number of columns of matrix ** */
/* Coded in C++14 */
#include<iostream>
using namespace std;
int Min(int a,int b,int c)
{
if(a<=b && a<=c)
{
return a;
}
else if(b<=a&& b<=c)
{
return b;
}
else
{
return c;
}
}
int main()
{
cout<<"\n Enter number of rows of matrix : ";
int m;
cin>>m;
cout<<"\n Enter number of columns of matrix : ";
int n;
cin>>n;
int Matrix[m][n];
for(int i=0;i<m;i++)
{
cout<<"\nEnter "<<n<<" elements of row "<<i+1<<" seprated by space\n";
for(int j=0;j<n;j++)
{
cin>>Matrix[i][j];
}
}
int CostMatrix[m][n];
//Base Condition
CostMatrix[0][0]=Matrix[0][0];
//Initializing First Row
for(int i=1;i<n;i++)
{
CostMatrix[0][i]=CostMatrix[0][i-1]+Matrix[0][i];
}
//Initializing First Column
for(int i=1;i<m;i++)
{
CostMatrix[i][0]=CostMatrix[i-1][0]+Matrix[i][0];
}
for(int i=1;i<m;i++)
{
for(int j=1;j<n;j++)
{
CostMatrix[i][j]=(Min(CostMatrix[i-1][j-1],CostMatrix[i-1][j],CostMatrix[i][j-1]))+Matrix[i][j];
}
}
cout<<"\n Minimum Sum Of Path is : "<<CostMatrix[m-1][n-1];
return 0;
}