Find minimum steps required to reach the end of a matrix | Set 2
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++
#include <bits/stdc++.h>
#define n 3
using namespace std;
int dp[n][n];
int v[n][n];
int minSteps( int i, int j, int arr[][n])
{
if (i == n - 1 and j == n - 1)
return 0;
if (i > n - 1 || j > n - 1)
return 9999999;
if (v[i][j])
return dp[i][j];
v[i][j] = 1;
dp[i][j] = 9999999;
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];
}
int 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
class GFG {
static int n = 3 ;
static int [][] dp = new int [n][n];
static int [][] v = new int [n][n];
static int minSteps( int i, int j, int arr[][])
{
if (i == n - 1 && j == n - 1 ) {
return 0 ;
}
if (i > n - 1 || j > n - 1 ) {
return 9999999 ;
}
if (v[i][j] == 1 ) {
return dp[i][j];
}
v[i][j] = 1 ;
dp[i][j] = 9999999 ;
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];
}
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);
}
}
}
|
Python3
import numpy as np
n = 3
dp = np.zeros((n,n))
v = np.zeros((n,n));
def minSteps(i, j, arr) :
if (i = = n - 1 and j = = n - 1 ) :
return 0 ;
if (i > n - 1 or j > n - 1 ) :
return 9999999 ;
if (v[i][j]) :
return dp[i][j];
v[i][j] = 1 ;
dp[i][j] = 9999999 ;
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];
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);
|
C#
using System;
class GFG
{
static int n = 3;
static int [,] dp = new int [n, n];
static int [,] v = new int [n, n];
static int minSteps( int i, int j, int [,]arr)
{
if (i == n - 1 && j == n - 1)
{
return 0;
}
if (i > n - 1 || j > n - 1)
{
return 9999999;
}
if (v[i, j] == 1)
{
return dp[i, j];
}
v[i, j] = 1;
dp[i, j] = 9999999;
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];
}
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);
}
}
}
|
Javascript
<script>
let n = 3;
let dp = new Array(n);
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 minSteps(i, j, arr)
{
if (i == n - 1 && j == n - 1) {
return 0;
}
if (i > n - 1 || j > n - 1) {
return 9999999;
}
if (v[i][j] == 1) {
return dp[i][j];
}
v[i][j] = 1;
dp[i][j] = 9999999;
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>
|
Time Complexity: O(n3).
Auxiliary Space: O(n*n)
Last Updated :
21 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...