|
| 1 | +/* |
| 2 | +Given a cost matrix mxn having a cost at each cell. Find the min cost that it will take to |
| 3 | +reach cell (m-1,n-1) from top left corner cell (0,0) if the only allowed directions to move are right, |
| 4 | +down and diagnal down |
| 5 | +
|
| 6 | +METHOD: |
| 7 | +In this case we cannot use greedy as each time we will try to find the min from the node and traverse that |
| 8 | +path, but we may end up with a node having max as the only option or min as the max number which may not |
| 9 | +be the shortest path |
| 10 | +
|
| 11 | +Hence we use DP: |
| 12 | +Assume the matrix is something like this: |
| 13 | +
|
| 14 | +10 3 4 |
| 15 | +5 6 7 |
| 16 | +13 4 11 |
| 17 | +
|
| 18 | +and we need to reach from 0,0 to n-1,m-1 |
| 19 | +
|
| 20 | +Therefore at each node I have three choices, recursive equation may look like this: |
| 21 | +
|
| 22 | +C(0,0) = min{ |
| 23 | + 10 + C(0,1), |
| 24 | + 10 + C(1,1), |
| 25 | + 10 + C(1,0) |
| 26 | +} |
| 27 | +
|
| 28 | +Hence if we convert it into a tree, we can see repeating sub problems. The number of unique problems are clearly |
| 29 | +n*m and c(i,j)// i can take n values and j can take m values (entire row and columns) |
| 30 | +
|
| 31 | +Now we make a matrix of size m*n and reverse engineer the solution. If we start from 0,0, it is the bigger |
| 32 | +problem we need to solve, but if we start from n-1,m-1, it is the smallest problem. Hence we try to construct |
| 33 | +the bottom most row, as |
| 34 | +
|
| 35 | +28 15 11 |
| 36 | +
|
| 37 | +as from cell in the middle it will take (4+11) 15 cost to reach 11 and similarly from cell 13 it will take(15+13) |
| 38 | +
|
| 39 | +Now for the next rows, we keep taking the min of the three ways we have been given in the question and 0,0 |
| 40 | +will be the answer. |
| 41 | +
|
| 42 | +Time complexity: O(mn) |
| 43 | +Space complexity: O(mn) |
| 44 | +
|
| 45 | +Here we can even construct the path by just finding min starting from 0,0 in this matrix |
| 46 | +We can also save the space complexity in case path is not required by just maintaing the values of two rows |
| 47 | +at a time at any given time. |
| 48 | +*/ |
| 49 | +#include <stdio.h> |
| 50 | +#include <stdlib.h> |
| 51 | + |
| 52 | +int min(int a, int b, int c){ |
| 53 | + return (a<b?(a<c?a:c):(b<c?b:c)); |
| 54 | +} |
| 55 | + |
| 56 | +int findCost(int rows, int columns,int arr[rows][columns]){ |
| 57 | + |
| 58 | + int cost[rows][columns]; |
| 59 | + |
| 60 | + int i,j; |
| 61 | + |
| 62 | + cost[rows-1][columns-1] = arr[rows-1][columns-1]; |
| 63 | + |
| 64 | + for(i=rows-1;i>=0;i--){ |
| 65 | + for(j=columns-1;j>=0;j--){ |
| 66 | + if(i == rows-1){ |
| 67 | + cost[i][j] = arr[i][j] + cost[i][j+1]; |
| 68 | + } |
| 69 | + else if(j == columns-1){ |
| 70 | + cost[i][j] = arr[i][j] + cost[i+1][j]; |
| 71 | + }else{ |
| 72 | + cost[i][j] = arr[i][j] + min(cost[i+1][j+1],cost[i][j+1],cost[i+1][j]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + //printing the array |
| 79 | + for(i=0;i<rows;i++){ |
| 80 | + for(j=0;j<columns;j++){ |
| 81 | + printf("%d ", cost[i][j]); |
| 82 | + } |
| 83 | + printf("\n"); |
| 84 | + } |
| 85 | + |
| 86 | + printf("the path is: \n"); |
| 87 | + i=0,j=0; |
| 88 | + while(i<=rows-1 && j<=columns-1){ |
| 89 | + printf("%d ", arr[i][j]); |
| 90 | + if(cost[i+1][j+1] < cost[i+1][j]){ |
| 91 | + if(cost[i+1][j+1] < cost[i][j+1]){ |
| 92 | + i = i+1; |
| 93 | + j = j+1; |
| 94 | + }else{ |
| 95 | + i = i; |
| 96 | + j = j+1; |
| 97 | + } |
| 98 | + }else{ |
| 99 | + if(cost[i+1][j] < cost[i][j+1]){ |
| 100 | + i = i+1; |
| 101 | + j = j; |
| 102 | + }else{ |
| 103 | + i = i; |
| 104 | + j = j+1; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return cost[0][0]; |
| 110 | +} |
| 111 | + |
| 112 | +int main(){ |
| 113 | + |
| 114 | + int cost[3][3] = { |
| 115 | + {10,3,4}, |
| 116 | + {5,6,17}, |
| 117 | + {13,4,11}, |
| 118 | + }; |
| 119 | + |
| 120 | + int rows = sizeof(cost)/sizeof(cost[0]); |
| 121 | + int columns = sizeof(cost[0])/sizeof(cost[0][0]); |
| 122 | + |
| 123 | + int total = findCost(rows,columns,cost); |
| 124 | + printf("total cost of reaching is %d\n", total); |
| 125 | + |
| 126 | + return 0; |
| 127 | +} |
| 128 | + |
0 commit comments