Find minimum steps required to reach the end of a matrix | Set – 1
Last Updated :
21 Dec, 2022
Given a 2D matrix consisting of positive integers, the task is to find the minimum number of steps required to reach the end(leftmost-bottom cell) of the matrix. If we are at cell (i, j) we can go to cells (i, j+arr[i][j]) or (i+arr[i][j], j). We can not go out of bounds. If no path exists, print -1.
Examples:
Input :
mat[][] = {{2, 1, 2},
{1, 1, 1},
{1, 1, 1}}
Output : 2
Explanation : The path will be {0, 0} -> {0, 2} -> {2, 2}
Thus, we are reaching end in two steps.
Input :
mat[][] = {{1, 1, 2},
{1, 1, 1},
{2, 1, 1}}
Output : 3
A simple solution is to explore all possible solutions. This will take exponential time.
Better approach: We can use dynamic programming to solve this problem in polynomial time.
Let’s decide the states of ‘dp’. We will build up our solution on 2d 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 only have two possible paths i.e. to cells {i, j+arr[i][j]} or {i+arr[i][j], j}.
A simple recurrence relation will be:
dp[i][j] = 1 + min(dp[i+arr[i][j]][j], dp[i][j+arr[i][j]])
Below is the implementation of the above idea:
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] = 1 + min(minSteps(i + arr[i][j], j, arr),
minSteps(i, j + arr[i][j], arr));
return dp[i][j];
}
int main()
{
int arr[n][n] = { { 2, 1, 2 },
{ 1, 1, 1 },
{ 1, 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] = 1 + Math.min(minSteps(i + arr[i][j], j, arr), minSteps(i, j + arr[i][j], arr));
return dp[i][j];
}
public static void main(String[] args)
{
int arr[][] = { { 2 , 1 , 2 },
{ 1 , 1 , 1 },
{ 1 , 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] = 1 + min (minSteps(i + arr[i][j], j, arr),
minSteps(i, j + arr[i][j], arr));
return dp[i][j];
arr = [ [ 2 , 1 , 2 ],
[ 1 , 1 , 1 ],
[ 1 , 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] = 1 + Math.Min(minSteps(i + arr[i,j], j, arr),
minSteps(i, j + arr[i,j], arr));
return dp[i, j];
}
static public void Main ()
{
int [,]arr = { { 2, 1, 2 },
{ 1, 1, 1 },
{ 1, 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);
for (let i = 0; i < n; i++)
{
dp[i] = new Array(n);
}
let v = new Array(n);
for (let i = 0; i < n; i++)
{
v[i] = new Array(n);
}
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] = 1 + Math.min(minSteps(i +
arr[i][j], j, arr), minSteps(i, j + arr[i][j], arr));
return dp[i][j];
}
let arr = [ [ 2, 1, 2 ],
[ 1, 1, 1 ],
[ 1, 1, 1 ] ];
let ans = minSteps(0, 0, arr);
if (ans >= 9999999) {
document.write(-1);
}
else {
document.write(ans);
}
</script>
|
Time Complexity: O(N2).
Auxiliary Space: O(N2)
Share your thoughts in the comments
Please Login to comment...