-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMinCostPath.cpp
More file actions
40 lines (34 loc) · 804 Bytes
/
MinCostPath.cpp
File metadata and controls
40 lines (34 loc) · 804 Bytes
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
/* A Naive recursive implementation of
MCP(Minimum Cost Path) problem */
public class GFG {
/* A utility function that returns
minimum of 3 integers */
static int min(int x, int y, int z)
{
if (x < y)
return (x < z) ? x : z;
else
return (y < z) ? y : z;
}
/* Returns cost of minimum cost path
from (0,0) to (m, n) in mat[R][C]*/
static int minCost(int cost[][], int m, int n)
{
if (n < 0 || m < 0)
return Integer.MAX_VALUE;
else if (m == 0 && n == 0)
return cost[m][n];
else
return cost[m][n]
+ min(minCost(cost, m - 1, n - 1),
minCost(cost, m - 1, n),
minCost(cost, m, n - 1));
}
// Driver code
public static void main(String args[])
{
int cost[][]
= { { 1, 2, 3 }, { 4, 8, 2 }, { 1, 5, 3 } };
System.out.print(minCost(cost, 2, 2));
}
}