# Minimum cost path in a Matrix by moving only on value difference of X

Given a matrix mat[][] and an integer X, the task is to find the minimum number of operations required to reach from . In each move, we can move either right or down in the matrix, but to move on the next cell in the matrix the value of the cell must be . In one operation the value at any cell can be decremented by 1.
Examples:

Input: mat[][] = {{8, 10, 14}, {5, 41, 19}, {10, 2, 25}}, X = 3
Output: 11
Explanation:
After performing the operations on the matrix:
7 10 13
5 41 16
10 2 19
Here the minimum required operation is 11.
8 => 7 = 1
14 => 13 = 1
19 => 16 = 3
25 => 19 = 6
Path: 7 => 10 => 13 => 16 => 19

Input: mat[][] = {{15, 153}, {135, 17}}, X = 3
Output: 125

Approach: The idea is to use Dynamic Programming to solve this problem. The general idea is to iterate over every possible cell of the matrix and find the number of operations required if the value at the current cell is not changed in the final path. If the value at the current cell is , Then the required value at the cell is to be . Similarly, we can compute the number of operations required recursively.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the  ` `// minimum number of operations  ` `// required to move from  ` `// (1, 1) to (N, M)  ` ` `  `#include   ` `using` `namespace` `std;  ` ` `  `const` `long` `long` `MAX = 1e18;  ` ` `  `long` `long` `n, m;  ` `vector<``long` `long``> v;  ` `long` `long` `dp;  ` ` `  `// Function to find the minimum  ` `// operations required to move  ` `// to bottom-right cell of matrix  ` `long` `long` `min_operation(``long` `long` `i,  ` `    ``long` `long` `j, ``long` `long` `val, ``long` `long` `x) {  ` `         `  `    ``// Condition to check if the  ` `    ``// current cell is the bottom-right  ` `    ``// cell of the matrix  ` `    ``if` `(i == n - 1 && j == m - 1) {  ` `        ``if` `(val > v[i][j]) {  ` `            ``return` `dp[i][j] = MAX;  ` `        ``}  ` `        ``else` `{  ` `            ``return` `dp[i][j] =  ` `                ``v[i][j] - val;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Condition to check if the  ` `    ``// current cell is out of  ` `    ``// matrix  ` `    ``if` `(i == n || j == m) {  ` `        ``return` `dp[i][j] = MAX;  ` `    ``}  ` `     `  `    ``// Condition to check if the  ` `    ``// current indices is already  ` `    ``// computed  ` `    ``if` `(dp[i][j] != -1) {  ` `        ``return` `dp[i][j];  ` `    ``}  ` `     `  `    ``// Condition to check that the  ` `    ``// movement with the current  ` `    ``// value is not possible  ` `    ``if` `(val > v[i][j]) {  ` `        ``return` `dp[i][j] = MAX;  ` `    ``}  ` `    ``long` `long` `tmp = v[i][j] - val;  ` `     `  `    ``// Recursive call to compute the  ` `    ``// number of operation required  ` `    ``// to compute the value  ` `    ``tmp += min(min_operation(i + 1,  ` `            ``j, val + x, x),  ` `            ``min_operation(i,  ` `            ``j + 1, val + x, x));  ` `    ``return` `dp[i][j] = tmp;  ` `}  ` ` `  `// Function to find the minimum  ` `// number of operations required  ` `// to reach the bottom-right cell  ` `long` `long` `solve(``long` `long` `x)  ` `{  ` `    ``long` `long` `ans = INT64_MAX;  ` `     `  `    ``// Loop to iterate over every  ` `    ``// possible cell of the matrix  ` `    ``for` `(``long` `long` `i = 0;  ` `        ``i < n; i++) {  ` ` `  `        ``for` `(``long` `long` `j = 0;  ` `            ``j < m; j++) {  ` ` `  `            ``long` `long` `val =  ` `                ``v[i][j] - x * (i + j);  ` ` `  `            ``memset``(dp, -1,  ` `                ``sizeof``(dp));  ` ` `  `            ``val = min_operation(  ` `                ``0, 0, val, x);  ` ` `  `            ``ans = min(ans, val);  ` `        ``}  ` `    ``}  ` `    ``return` `ans;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``n = 2, m = 2;  ` `    ``long` `long` `x = 3;  ` `     `  `    ``v = { 15, 153 };  ` `    ``v = { 135, 17 };  ` `     `  `    ``// Function Call  ` `    ``cout << solve(x) << endl;  ` `    ``return` `0;  ` `}  `

## Java

 `// Java implementation to find the   ` `// minimum number of operations  ` `// required to move from   ` `// (1, 1) to (N, M)  ` `import` `java.lang.*; ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `static` `final` `long` `MAX = (``long``)1e18; ` `static` `long` `n, m; ` ` `  `static` `List> v = ``new` `ArrayList<>(``151``); ` `static` `long``[][] dp = ``new` `long``[``151``][``151``]; ` ` `  `// Function to find the minimum  ` `// operations required to move ` `// to bottom-right cell of matrix ` `static` `long` `min_operation(``long` `i, ``long` `j,  ` `                          ``long` `val, ``long` `x) ` `{ ` `     `  `    ``// Condition to check if the  ` `    ``// current cell is the bottom-right ` `    ``// cell of the matrix ` `    ``if` `(i == n - ``1` `&& j == m - ``1``) ` `    ``{ ` `        ``if` `(val > v.get((``int``)i).get((``int``)j)) ` `        ``{ ` `            ``return` `dp[(``int``)i][(``int``)j] = MAX; ` `        ``} ` `        ``else` `        ``{ ` `            ``return` `dp[(``int``)i][(``int``)j] =  ` `                ``v.get((``int``)i).get((``int``)j) - val; ` `        ``} ` `    ``} ` `     `  `    ``// Condition to check if the ` `    ``// current cell is out of  ` `    ``// matrix ` `    ``if` `(i == n || j == m)  ` `    ``{ ` `        ``return` `dp[(``int``)i][(``int``)j] = MAX; ` `    ``} ` `     `  `    ``// Condition to check if the ` `    ``// current indices is already  ` `    ``// computed  ` `    ``if` `(dp[(``int``)i][(``int``)j] != -``1``)  ` `    ``{ ` `        ``return` `dp[(``int``)i][(``int``)j]; ` `    ``} ` `     `  `    ``// Condition to check that the  ` `    ``// movement with the current  ` `    ``// value is not possible  ` `    ``if` `(val > v.get((``int``)i).get((``int``)j))  ` `    ``{ ` `        ``return` `dp[(``int``)i][(``int``)j] = MAX; ` `    ``} ` `    ``long` `tmp = v.get((``int``)i).get((``int``)j) - val; ` `     `  `    ``// Recursive call to compute the  ` `    ``// number of operation required ` `    ``// to compute the value ` `    ``tmp += Math.min(min_operation(i + ``1``,  ` `                             ``j, val + x, x), ` `                    ``min_operation(i, j + ``1``,  ` `                                   ``val + x, x)); ` `                                    `  `    ``return` `dp[(``int``)i][(``int``)j] = tmp; ` `} ` ` `  `// Function to find the minimum ` `// number of operations required ` `// to reach the bottom-right cell ` `static` `long` `solve(``long` `x) ` `{ ` `    ``long` `ans = Long.MAX_VALUE; ` `     `  `    ``// Loop to iterate over every  ` `    ``// possible cell of the matrix ` `    ``for``(``long` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``for``(``long` `j = ``0``; j < m; j++) ` `        ``{ ` `            ``long` `val = v.get((``int``)i).get((``int``)j) - ` `                       ``x * (i + j); ` `     `  `            ``for``(``int` `k = ``0``; k < dp.length; k++) ` `                ``for``(``int` `l = ``0``; l < dp[k].length; l++) ` `                    ``dp[k][l] = -``1``; ` `                     `  `            ``val = min_operation(0l, 0l, val, x); ` `            ``ans = Math.min(ans, val); ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``n = ``2``; m = ``2``; ` `    ``long` `x = ``3``; ` `     `  `    ``v.add(Arrays.asList(15l, 153l)); ` `    ``v.add(Arrays.asList(135l, 17l)); ` `     `  `    ``// Function call ` `    ``System.out.println(solve(x)); ` `} ` `} ` ` `  `// This code is contributed by offbeat `

## Python3

 `# Python3 implementation to find the  ` `# minimum number of operations  ` `# required to move from  ` `# (1, 1) to (N, M)  ` `MAX` `=` `1e18` `v ``=` `[[ ``0``, ``0``]] ``*` `(``151``)  ` `dp ``=` `[[``-``1` `for` `i ``in` `range``(``151``)]  ` `        ``for` `i ``in` `range``(``151``)]  ` ` `  `# Function to find the minimum  ` `# operations required to move  ` `# to bottom-right cell of matrix  ` `def` `min_operation(i, j, val, x):  ` ` `  `    ``# Condition to check if the  ` `    ``# current cell is the bottom-right  ` `    ``# cell of the matrix  ` `    ``if` `(i ``=``=` `n ``-` `1` `and` `j ``=``=` `m ``-` `1``):  ` `        ``if` `(val > v[i][j]):  ` `            ``dp[i][j] ``=` `MAX` `            ``return` `MAX` ` `  `        ``else``:  ` `            ``dp[i][j] ``=` `v[i][j] ``-` `val  ` `            ``return` `dp[i][j]  ` ` `  `    ``# Condition to check if the  ` `    ``# current cell is out of  ` `    ``# matrix  ` `    ``if` `(i ``=``=` `n ``or` `j ``=``=` `m):  ` `        ``dp[i][j] ``=` `MAX` `        ``return` `MAX` ` `  `    ``# Condition to check if the  ` `    ``# current indices is already  ` `    ``# computed  ` `    ``if` `(dp[i][j] !``=` `-``1``):  ` `        ``return` `dp[i][j]  ` ` `  `    ``# Condition to check that the  ` `    ``# movement with the current  ` `    ``# value is not possible  ` `    ``if` `(val > v[i][j]):  ` `        ``dp[i][j] ``=` `MAX` `        ``return` `MAX` ` `  `    ``tmp ``=` `v[i][j] ``-` `val  ` ` `  `    ``# Recursive call to compute the  ` `    ``# number of operation required  ` `    ``# to compute the value  ` `    ``tmp ``+``=` `min``(min_operation(i ``+` `1``, j,  ` `                        ``val ``+` `x, x),  ` `            ``min_operation(i, j ``+` `1``,  ` `                            ``val ``+` `x, x))  ` `    ``dp[i][j] ``=` `tmp  ` ` `  `    ``return` `tmp  ` ` `  `# Function to find the minimum  ` `# number of operations required  ` `# to reach the bottom-right cell  ` `def` `solve(x):  ` `     `  `    ``ans ``=` `10` `*``*` `19` ` `  `    ``# Loop to iterate over every  ` `    ``# possible cell of the matrix  ` `    ``for` `i ``in` `range``(n):  ` `        ``for` `j ``in` `range``(m):  ` `            ``val ``=` `v[i][j] ``-` `x ``*` `(i ``+` `j)  ` ` `  `            ``for` `ii ``in` `range``(``151``):  ` `                ``for` `jj ``in` `range``(``151``):  ` `                    ``dp[ii][jj] ``=` `-``1` ` `  `            ``val ``=` `min_operation(``0``, ``0``, val, x)  ` `            ``ans ``=` `min``(ans, val)  ` `    ``return` `ans  ` ` `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``:  ` `     `  `    ``n ``=` `2` `    ``m ``=` `2` `    ``x ``=` `3` ` `  `    ``v[``0``] ``=` `[ ``15``, ``153` `]  ` `    ``v[``1``] ``=` `[ ``135``, ``17` `]  ` ` `  `    ``# Function Call  ` `    ``print``(solve(x))  ` ` `  `# This code is contributed by mohit kumar 29      `

Output:

```125
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29, offbeat