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 19Here the minimum required operation is 11.

8 => 7 = 1

14 => 13 = 1

19 => 16 = 3

25 => 19 = 6Path: 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 <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `long` `long` `MAX = 1e18; ` ` ` `long` `long` `n, m; ` `vector<` `long` `long` `> v[151]; ` `long` `long` `dp[151][151]; ` ` ` `// 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[0] = { 15, 153 }; ` ` ` `v[1] = { 135, 17 }; ` ` ` ` ` `// Function Call ` ` ` `cout << solve(x) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Minimum odd cost path in a matrix
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Minimum cost to sort a matrix of numbers from 0 to n^2 - 1
- Minimum cost to convert 3 X 3 matrix into magic square
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Minimum changes required to make each path in a matrix palindrome
- Find the minimum difference path from (0, 0) to (N-1, M-1)
- Min Cost Path | DP-6
- C Program for Min Cost Path
- Java Program for Min Cost Path
- Python Program for Min Cost Path
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Minimum steps required to convert the matrix into lower hessenberg matrix
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Maximise matrix sum by following the given Path

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.