# Check if possible to cross the matrix with given power

Given a matrix of N X M, each cell consist of an integer. We have initial power of K and we are allowed to move right, down or diagonal. When we move to any cell, we absorb mat[i][j] value and loose that much amount from your power. If our power became less than 0 at any time, we cannot move further from that point. Now, our task is to find if there is any path from (1, 1) to (n, m) that we can cover with power k. If possible, output the maximum value we can absorb, else if there is no path print “-1”.

Examples :

```Input : N = 3, M = 3, K = 7
mat[][] = { { 2, 3, 1 },
{ 6, 1, 9 },
{ 8, 2, 3 } };
Output : 6
Path (1, 1) -> (2, 2) -> (3, 3) to complete
journey to absorb 6 value.

Input : N = 3, M = 4, K = 9
mat[][] = {
{ 2, 3, 4, 1 },
{ 6, 5, 5, 3 },
{ 5, 2, 3, 4 }
};
Output : -1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is to use Dynamic Programming to solve the problem.
Approach :

• Declare a boolean 3D matrix, say dp[ ][ ][ ], with N*M*(K+1) dimension such that dp[ i ][ j ][ k ] is true if it possible to reach the square in the ith row and jth column with exactly k value collected so far.
• We can write the recurrence dp[ i ][ j ][ k ] = true if either
```dp[i-1][j][k-mat[i][j]] or
dp[i][j-1][k-mat[i][j]] or
dp[i-1][j-1][k-mat[i][j]] ```

i.e the three possible moves we could have.

• We have base case dp[0][0][0] be true.
• The answer is -2 if dp[n-1][m-1][k] is false for all k between 0 and k+1.
• Otherwise, the answer is the maximum k such that dp[n-1][m-1][k] is true.

Below is C++ implementation of this approach :

## CPP

 `// CPP program to find if it is possible to cross ` `// the matrix with given power ` `#include ` `#define N 105 ` `#define R 3 ` `#define C 4 ` `using` `namespace` `std; ` ` `  `int` `maximumValue(``int` `n, ``int` `m, ``int` `p, ``int` `grid[R][C]) ` `{ ` `    ``bool` `dp[N][N][N]; ` ` `  `    ``// Initializing array dp with false value. ` `    ``for` `(``int` `i = 0; i < N; i++) { ` `        ``for` `(``int` `j = 0; j < N; j++) { ` `            ``for` `(``int` `k = 0; k < N; k++) ` `                ``dp[i][j][k] = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// For each value of dp[i][j][k] ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``for` `(``int` `j = 0; j < m; j++) { ` `            ``for` `(``int` `k = grid[i][j]; k <= p; k++) { ` ` `  `                ``// For first cell and for each value of k ` `                ``if` `(i == 0 && j == 0) { ` `                    ``if` `(k == grid[i][j]) ` `                        ``dp[i][j][k] = ``true``; ` `                ``} ` ` `  `                ``// For first cell of each row ` `                ``else` `if` `(i == 0) { ` `                    ``dp[i][j][k] = (dp[i][j][k] ||  ` `                        ``dp[i][j - 1][k - grid[i][j]]); ` `                ``} ` ` `  `                ``// For first cell of each column ` `                ``else` `if` `(j == 0) { ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - 1][j][k - grid[i][j]]); ` `                ``} ` ` `  `                ``// For rest of the cell ` `                ``else` `{ ` ` `  `                    ``// Down movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i][j - 1][k - grid[i][j]]); ` ` `  `                    ``// Right movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - 1][j][k - grid[i][j]]); ` ` `  `                    ``// Diagonal movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - 1][j - 1][k - grid[i][j]]); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Finding maximum k. ` `    ``int` `ans = 0; ` `    ``for` `(ans = k; ans >= 0; ans--) ` `        ``if` `(dp[n - 1][m - 1][ans]) ` `            ``break``; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 3, m = 4, p = 9; ` `    ``int` `grid[R][C] = { ` `        ``{ 2, 3, 4, 1 }, ` `        ``{ 6, 5, 5, 3 }, ` `        ``{ 5, 2, 3, 4 } ` `    ``}; ` ` `  `    ``cout << maximumValue(n, m, p, grid) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find if it  ` `// is possible to cross the matrix ` `// with given power ` `class` `GFG ` `{ ` `     `  `static` `final` `int` `N = ``105``; ` `static` `final` `int` `R = ``3``; ` `static` `final` `int` `C = ``4``; ` ` `  `static` `int` `maximumValue(``int` `n, ``int` `m, ``int` `p, ` `                                ``int` `grid[][]) ` `{ ` `    ``boolean` `dp[][][] = ``new` `boolean``[N][N][N]; ` `    ``int` `i, j, k; ` ` `  `    ``// Initializing array dp with false value. ` `    ``for` `(i = ``0``; i < N; i++) { ` `        ``for` `(j = ``0``; j < N; j++) { ` `            ``for` `(k = ``0``; k < N; k++) ` `                ``dp[i][j][k] = ``false``; ` `        ``} ` `    ``} ` ` `  `    ``// For each value of dp[i][j][k] ` `    ``for` `(i = ``0``; i < n; i++) { ` `        ``for` `(j = ``0``; j < m; j++) { ` `            ``for` `(k = grid[i][j]; k <= p; k++) { ` ` `  `                ``// For first cell and for  ` `                ``// each value of k ` `                ``if` `(i == ``0` `&& j == ``0``) { ` `                    ``if` `(k == grid[i][j]) ` `                        ``dp[i][j][k] = ``true``; ` `                ``} ` ` `  `                ``// For first cell of each row ` `                ``else` `if` `(i == ``0``) { ` `                    ``dp[i][j][k] = (dp[i][j][k] ||  ` `                        ``dp[i][j - ``1``][k - grid[i][j]]); ` `                ``} ` ` `  `                ``// For first cell of each column ` `                ``else` `if` `(j == ``0``) { ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - ``1``][j][k - grid[i][j]]); ` `                ``} ` ` `  `                ``// For rest of the cell ` `                ``else` `{ ` ` `  `                    ``// Down movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i][j - ``1``][k - grid[i][j]]); ` ` `  `                    ``// Right movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - ``1``][j][k - grid[i][j]]); ` ` `  `                    ``// Diagonal movement. ` `                    ``dp[i][j][k] = (dp[i][j][k] || ` `                        ``dp[i - ``1``][j - ``1``][k - grid[i][j]]); ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` `    ``k = p; ` `     `  `    ``// Finding maximum k. ` `    ``int` `ans = ``0``; ` `    ``for` `(ans = k; ans >= ``0``; ans--) ` `        ``if` `(dp[n - ``1``][m - ``1``][ans]) ` `            ``break``; ` ` `  `    ``return` `ans; ` `} ` ` `  ` `  `// Driver code  ` `public` `static` `void` `main (String[] args) ` `{ ` `    ``int` `n = ``3``, m = ``4``, p = ``9``; ` `    ``int` `grid[][] = {{ ``2``, ``3``, ``4``, ``1` `}, ` `                    ``{ ``6``, ``5``, ``5``, ``3` `}, ` `                    ``{ ``5``, ``2``, ``3``, ``4` `}}; ` ` `  `    ``System.out.println(maximumValue(n, m, p, grid)); ` `} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## C#

 `// C# program to find if it ` `// is possible to cross the matrix ` `// with given power ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `N = 105; ` `    ``// static int R = 3; ` `    ``// static int C = 4; ` ` `  `    ``static` `int` `maximumValue(``int` `n, ``int` `m, ``int` `p, ` `                                   ``int``[, ] grid) ` `    ``{ ` `        ``bool``[,, ] dp = ``new` `bool``[N, N, N]; ` `        ``int` `i, j, k; ` ` `  `        ``// Initializing array dp with false value. ` `        ``for` `(i = 0; i < N; i++) { ` `            ``for` `(j = 0; j < N; j++) { ` `                ``for` `(k = 0; k < N; k++) ` `                    ``dp[i, j, k] = ``false``; ` `            ``} ` `        ``} ` ` `  `        ``// For each value of dp[i][j][k] ` `        ``for` `(i = 0; i < n; i++) { ` `            ``for` `(j = 0; j < m; j++) { ` `                ``for` `(k = grid[i, j]; k <= p; k++) { ` ` `  `                    ``// For first cell and for ` `                    ``// each value of k ` `                    ``if` `(i == 0 && j == 0) { ` `                        ``if` `(k == grid[i, j]) ` `                            ``dp[i, j, k] = ``true``; ` `                    ``} ` ` `  `                    ``// For first cell of each row ` `                    ``else` `if` `(i == 0) { ` `                        ``dp[i, j, k] = (dp[i, j, k] ||  ` `                        ``dp[i, j - 1, k - grid[i, j]]); ` `                    ``} ` ` `  `                    ``// For first cell of each column ` `                    ``else` `if` `(j == 0) { ` `                        ``dp[i, j, k] = (dp[i, j, k] ||  ` `                        ``dp[i - 1, j, k - grid[i, j]]); ` `                    ``} ` ` `  `                    ``// For rest of the cell ` `                    ``else` `{ ` ` `  `                        ``// Down movement. ` `                        ``dp[i, j, k] = (dp[i, j, k] ||  ` `                        ``dp[i, j - 1, k - grid[i, j]]); ` ` `  `                        ``// Right movement. ` `                        ``dp[i, j, k] = (dp[i, j, k] ||  ` `                        ``dp[i - 1, j, k - grid[i, j]]); ` ` `  `                        ``// Diagonal movement. ` `                        ``dp[i, j, k] = (dp[i, j, k] || ` `                        ``dp[i - 1, j - 1, k - grid[i, j]]); ` `                    ``} ` `                ``} ` `            ``} ` `        ``} ` `        ``k = p; ` ` `  `        ``// Finding maximum k. ` `        ``int` `ans = 0; ` `        ``for` `(ans = k; ans >= 0; ans--) ` `            ``if` `(dp[n - 1, m - 1, ans]) ` `                ``break``; ` ` `  `        ``return` `ans; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 3, m = 4, p = 9; ` `        ``int``[, ] grid = { { 2, 3, 4, 1 }, ` `                         ``{ 6, 5, 5, 3 }, ` `                         ``{ 5, 2, 3, 4 } }; ` ` `  `        ``Console.WriteLine(maximumValue(n, m, p, grid)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

Output:

```-1
```

Time Complexity: O(n3)

This article is contributed by anuj0503. 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.

My Personal Notes arrow_drop_up