Open In App

Check if possible to cross the matrix with given power

Improve
Improve
Like Article
Like
Save
Share
Report

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 lose 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

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 implementation of this approach : 

CPP




// CPP program to find if it is possible to cross
// the matrix with given power
#include <bits/stdc++.h>
#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];
    int i,j,k;
    // 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
import java.util.*;
import java.io.*;
 
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.


Python3




# Python3 program to find if it is possible to cross
# the matrix with given power
N = 105
R = 3
C = 4
 
def maximumValue(n, m, p, grid):
     
    dp = [[[False for i in range(N)] for j in range(N)] for k in range(N)]
     
    # For each value of dp[i][j][k]
    for i in range(n):
        for j in range(m):
            k = grid[i][j]
            while(k <= p):
                 
                # For first cell and for each value of k
                if (i == 0 and j == 0):
                    if (k == grid[i][j]):
                        dp[i][j][k] = True
                 
                # For first cell of each row
                elif (i == 0):
                    dp[i][j][k] = (dp[i][j][k] or
                    dp[i][j - 1][k - grid[i][j]])
                 
                # For first cell of each column
                elif (j == 0):
                    dp[i][j][k] = (dp[i][j][k] or
                    dp[i - 1][j][k - grid[i][j]])
                     
                # For rest of the cell
                else:
                     
                    # Down movement.
                    dp[i][j][k] = (dp[i][j][k] or
                    dp[i][j - 1][k - grid[i][j]])
                     
                    # Right movement.
                    dp[i][j][k] = (dp[i][j][k] or
                    dp[i - 1][j][k - grid[i][j]])
                     
                    # Diagonal movement.
                    dp[i][j][k] = (dp[i][j][k] or
                    dp[i - 1][j - 1][k - grid[i][j]])
                k += 1
                 
    # Finding maximum k.
    ans = k
    while(ans >= 0):
        if (dp[n - 1][m - 1][ans]):
            break
        ans -= 1
     
    return ans
 
# Driver Code
n = 3
m = 4
p = 9
grid = [[2, 3, 4, 1],[6, 5, 5, 3 ],[5, 2, 3, 4]]
 
print(maximumValue(n, m, p, grid))
 
# This code is contributed by shubhamsingh10


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.


Javascript




<script>
 
// JavaScript program to find if it is possible to cross
// the matrix with given power
const N = 105
const R = 3
const C = 4
 
function maximumValue(n, m, p, grid)
{
    let dp = new Array(N).fill(0).map(()=>new Array(N).fill(0).map(()=>new Array(N)));
 
    let 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]]);
                }
            }
        }
    }
 
    // Finding maximum k.
    let ans = 0;
    for (ans = k; ans >= 0; ans--)
        if (dp[n - 1][m - 1][ans])
            break;
 
    return ans;
}
 
// Driver Code
 
let n = 3, m = 4, p = 9;
let grid = [
    [ 2, 3, 4, 1 ],
    [ 6, 5, 5, 3 ],
    [ 5, 2, 3, 4 ]
];
 
document.write(maximumValue(n, m, p, grid),"</br>");
 
// This code is contributed by shinjanpatra
 
</script>


Output

-1

Time Complexity: O(n3



Last Updated : 20 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads