-
Notifications
You must be signed in to change notification settings - Fork 313
/
min_cost_path.c
48 lines (43 loc) · 898 Bytes
/
min_cost_path.c
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
#include<stdio.h>
int p,q,grid[100][100];
#define minval(a,b,c) (a<b)?((a<c)?a:c):((b<c)?b:c)
#define min(a,b) (a,b)?a:b
int mincost(int m,int n)
{
int dp[100][100];
int i,j;
for(i=0;i<p;i++)
for(j=0;j<q;j++)
dp[i][j]=0;
dp[0][0]=grid[0][0];
for(i=1;i<p;i++)
dp[i][0]=dp[i-1][0]+grid[i][0];
for(j=1;j<q;j++)
dp[0][j]=dp[0][j-1]+grid[0][j];
for(i=1;i<p;i++)
{
for(j=1;j<q;j++)
{
dp[i][j]=minval(dp[i-1][j-1],dp[i][j-1],dp[i-1][j]);
dp[i][j]+=grid[i][j];
}
}
return dp[m][n];
}
int main()
{
int m,n,i,j;
printf("Enter the no.of rows and columns:");
scanf("%d%d",&p,&q);
printf("Enter the cost in the grid:");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&grid[i][j]);
printf("Enter the position:\n");
scanf("%d%d",&m,&n);
if(m > (p-1) || n > (q-1))
printf("invalid postion");
else
printf("%d",mincost(m,n));
return 0;
}