File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ /* A Naive recursive implementation of
2+ MCP(Minimum Cost Path) problem */
3+ public class GFG {
4+
5+ /* A utility function that returns
6+ minimum of 3 integers */
7+ static int min (int x , int y , int z )
8+ {
9+ if (x < y )
10+ return (x < z ) ? x : z ;
11+ else
12+ return (y < z ) ? y : z ;
13+ }
14+
15+ /* Returns cost of minimum cost path
16+ from (0,0) to (m, n) in mat[R][C]*/
17+ static int minCost (int cost [][], int m ,
18+ int n )
19+ {
20+ if (n < 0 || m < 0 )
21+ return Integer .MAX_VALUE ;
22+ else if (m == 0 && n == 0 )
23+ return cost [m ][n ];
24+ else
25+ return cost [m ][n ] +
26+ min ( minCost (cost , m -1 , n -1 ),
27+ minCost (cost , m -1 , n ),
28+ minCost (cost , m , n -1 ) );
29+ }
30+
31+ // Driver code
32+ public static void main (String args [])
33+ {
34+
35+ int cost [][] = { {1 , 2 , 3 },
36+ {4 , 8 , 2 },
37+ {1 , 5 , 3 } };
38+
39+ System .out .print (minCost (cost , 2 , 2 ));
40+ }
41+ }
42+
43+ // This code is contributed by Sam007
You can’t perform that action at this time.
0 commit comments