Given a 2D matrix consisting of positive integers, the task is to find the minimum number of steps required to reach the end of the matrix. If we are at cell (i, j) then we can go to all the cells represented by (i + X, j + Y) such that X ? 0, Y ? 0 and X + Y = arr[i][j]. If no path exists then print -1.
Examples:
Input: arr[][] = {Â
{4, 1, 1},Â
{1, 1, 1},Â
{1, 1, 1}}Â
Output: 1Â
The path will be from {0, 0} -> {2, 2} as manhattan distanceÂ
between two is 4.Â
Thus, we are reaching there in 1 step.Input: arr[][] = {Â
{1, 1, 2},Â
{1, 1, 1},Â
{2, 1, 1}}Â
Output: 3Â
A simple solution will be to explore all possible solutions which will take exponential time.
An efficient solution is to use dynamic programming to solve this problem in polynomial time. Lets decide the states of dp.Â
Let’s say we are at cell (i, j). We will try to find the minimum number of steps required to reach the cell (n – 1, n – 1) from this cell.Â
We have arr[i][j] + 1 possible path.
The recurrence relation will beÂ
dp[i][j] = 1 + min(dp[i][j + arr[i][j]], dp[i + 1][j + arr[i][j] – 1], …., dp[i + arr[i][j]][j])Â
To reduce the number of terms in recurrence relation, we can put an upper bound on the values of X and Y. How?Â
We know that i + X < N. Thus, X < N – i otherwise they would go out of bounds.Â
Similarly, Y < N – j
0 ? Y < N – j …(1)Â
X + Y = arr[i][j] …(2)Â
Substituting value of Y from second into first, we getÂ
X ? arr[i][j] + j – N + 1Â
From above we get another lower bound on constraint of X i.e. X ? arr[i][j] + j – N + 1.Â
So, new lower bound on X becomes X ? max(0, arr[i][j] + j – N + 1).Â
Also X ? min(arr[i][j], N – i – 1).
Our recurrence relation optimizes toÂ
dp[i][j] = 1 + min(dp[i + max(0, arr[i][j] + j – N + 1)][j + arr[i][j] – max(0, arr[i][j] + j – N + 1)], …., dp[i + min(arr[i][j], N – i – 1)][j + arr[i][j] – min(arr[i][j], N – i – 1)])Â
Below is the implementation of the above approach:Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>#define n 3using namespace std;Â
// 2d array to store// states of dpint dp[n][n];Â
// Array to determine whether// a state has been solved beforeint v[n][n];Â
// Function to return the minimum steps requiredint minSteps(int i, int j, int arr[][n]){Â
    // Base cases    if (i == n - 1 and j == n - 1)        return 0;Â
    if (i > n - 1 || j > n - 1)        return 9999999;Â
    // If a state has been solved before    // it won't be evaluated again    if (v[i][j])        return dp[i][j];Â
    v[i][j] = 1;    dp[i][j] = 9999999;Â
    // Recurrence relation    for (int k = max(0, arr[i][j] + j - n + 1);         k <= min(n - i - 1, arr[i][j]); k++) {        dp[i][j] = min(dp[i][j], minSteps(i + k, j + arr[i][j] - k, arr));    }Â
    dp[i][j]++;Â
    return dp[i][j];}Â
// Driver codeint main(){    int arr[n][n] = { { 4, 1, 2 },                      { 1, 1, 1 },                      { 2, 1, 1 } };Â
    int ans = minSteps(0, 0, arr);    if (ans >= 9999999)        cout << -1;    else        cout << ans;Â
    return 0;} |
Java
// Java implementation of the approachclass GFG {Â
    static int n = 3;Â
    // 2d array to store    // states of dp    static int[][] dp = new int[n][n];Â
    // Array to determine whether    // a state has been solved before    static int[][] v = new int[n][n];Â
    // Function to return the minimum steps required    static int minSteps(int i, int j, int arr[][])    {Â
        // Base cases        if (i == n - 1 && j == n - 1) {            return 0;        }Â
        if (i > n - 1 || j > n - 1) {            return 9999999;        }Â
        // If a state has been solved before        // it won't be evaluated again        if (v[i][j] == 1) {            return dp[i][j];        }Â
        v[i][j] = 1;        dp[i][j] = 9999999;Â
        // Recurrence relation        for (int k = Math.max(0, arr[i][j] + j - n + 1);             k <= Math.min(n - i - 1, arr[i][j]); k++) {            dp[i][j] = Math.min(dp[i][j],                                minSteps(i + k, j + arr[i][j] - k, arr));        }Â
        dp[i][j]++;Â
        return dp[i][j];    }Â
    // Driver code    public static void main(String[] args)    {        int arr[][] = { { 4, 1, 2 },                        { 1, 1, 1 },                        { 2, 1, 1 } };Â
        int ans = minSteps(0, 0, arr);        if (ans >= 9999999) {            System.out.println(-1);        }        else {            System.out.println(ans);        }    }}Â
// This code contributed by Rajput-Ji |
Python3
# Python3 implementation of the approach Â
import numpy as np n = 3Â
# 2d array to store # states of dp dp = np.zeros((n,n))Â
# Array to determine whether # a state has been solved before v = np.zeros((n,n)); Â
# Function to return the minimum steps required def minSteps(i, j, arr) : Â
    # Base cases     if (i == n - 1 and j == n - 1) :        return 0; Â
    if (i > n - 1 or j > n - 1) :        return 9999999; Â
    # If a state has been solved before     # it won't be evaluated again     if (v[i][j]) :        return dp[i][j]; Â
    v[i][j] = 1;     dp[i][j] = 9999999; Â
    # Recurrence relation     for k in range(max(0, arr[i][j] + j - n + 1),min(n - i - 1, arr[i][j]) + 1) :        dp[i][j] = min(dp[i][j], minSteps(i + k, j + arr[i][j] - k, arr));      Â
    dp[i][j] += 1; Â
    return dp[i][j]; Â
Â
# Driver code if __name__ == "__main__" : Â
    arr = [             [ 4, 1, 2 ],             [ 1, 1, 1 ],             [ 2, 1, 1 ]             ]; Â
    ans = minSteps(0, 0, arr);     if (ans >= 9999999) :        print(-1);     else :        print(ans); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{Â Â Â Â static int n = 3;Â
    // 2d array to store    // states of dp    static int[,] dp = new int[n, n];Â
    // Array to determine whether    // a state has been solved before    static int[,] v = new int[n, n];Â
    // Function to return the minimum steps required    static int minSteps(int i, int j, int [,]arr)    {Â
        // Base cases        if (i == n - 1 && j == n - 1)        {            return 0;        }Â
        if (i > n - 1 || j > n - 1)         {            return 9999999;        }Â
        // If a state has been solved before        // it won't be evaluated again        if (v[i, j] == 1)         {            return dp[i, j];        }Â
        v[i, j] = 1;        dp[i, j] = 9999999;Â
        // Recurrence relation        for (int k = Math.Max(0, arr[i,j] + j - n + 1);            k <= Math.Min(n - i - 1, arr[i,j]); k++)        {            dp[i,j] = Math.Min(dp[i,j],                                minSteps(i + k, j + arr[i,j] - k, arr));        }Â
        dp[i,j]++;Â
        return dp[i,j];    }Â
    // Driver code    static public void Main ()    {        int [,]arr = { { 4, 1, 2 },                        { 1, 1, 1 },                        { 2, 1, 1 } };Â
        int ans = minSteps(0, 0, arr);        if (ans >= 9999999)         {            Console.WriteLine(-1);        }        else        {            Console.WriteLine(ans);        }    }}Â
// This code contributed by ajit. |
Javascript
<script>       // Javascript implementation of the approach         let n = 3;       // 2d array to store    // states of dp    let dp = new Array(n);       // Array to determine whether    // a state has been solved before    let v = new Array(n);    for(let i = 0; i < n; i++)    {        dp[i] = new Array(n);        v[i] = new Array(n);        for(let j = 0; j < n; j++)        {            dp[i][j] = 0;            v[i][j] = 0;        }    }       // Function to return the minimum steps required    function minSteps(i, j, arr)    {           // Base cases        if (i == n - 1 && j == n - 1) {            return 0;        }           if (i > n - 1 || j > n - 1) {            return 9999999;        }           // If a state has been solved before        // it won't be evaluated again        if (v[i][j] == 1) {            return dp[i][j];        }           v[i][j] = 1;        dp[i][j] = 9999999;           // Recurrence relation        for (let k = Math.max(0, arr[i][j] + j - n + 1);             k <= Math.min(n - i - 1, arr[i][j]); k++) {            dp[i][j] = Math.min(dp[i][j],                                minSteps(i + k, j + arr[i][j] - k, arr));        }           dp[i][j]++;           return dp[i][j];    }         let arr = [ [ 4, 1, 2 ],               [ 1, 1, 1 ],               [ 2, 1, 1 ] ];       let ans = minSteps(0, 0, arr);    if (ans >= 9999999) {      document.write(-1);    }    else {      document.write(ans);    }Â
</script> |
1
Time Complexity: O(n3).
Auxiliary Space: O(n*n)Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
