Given a m x n matrix mat[][] containing positive integers. The problem is to reach to the cell (m-1, n-1) from the cell (0, 0) by following the given constraints. From a cell (i, j) one can move ‘exactly’ a distance of ‘mat[i][j]’ to the right (in the same row) or to below (in the same column) only if the movement takes to a cell within matrix boundaries.
For example: Given mat[1][1] = 4, then one can move to cells mat[1][5] and mat[5][1] only if these cells exists in the matrix. Following the constraints check whether one can reach cell (m-1, n-1) from (0, 0). 1If one can reach then print the minimum number of cells required to be covered during the movement else print “-1”.
Examples:
Input : mat[][] = { {2, 3, 2, 1, 4},
{3, 2, 5, 8, 2},
{1, 1, 2, 2, 1} }
Output : 4
The movement and cells covered are as follows:
(0, 0)->(0, 2)
|
(2, 2)->(2, 4)
Input : mat[][] = { {2, 4, 2},
{5, 3, 8},
{1, 1, 1} }
Output : 3
Approach:
- Initialize a 2D array dp of size m x n with all values as INT_MAX.
- Set dp[0][0] to 1, as the first cell is already reached.
- Iterate through each cell of the matrix, and check if the current cell can be reached from any cell already reached, i.e., if dp[i][j] != INT_MAX. If it can be reached, update the minimum cells required to reach the current cell from the previous cell by checking the cells to the right and bottom of the current cell.
- Return dp[m-1][n-1] if it is not equal to INT_MAX, else return -1.
Algorithm: A dynamic programming approach is given below:

Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100
int minCells( int mat[SIZE][SIZE], int m, int n)
{
int dp[m][n];
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
dp[i][j] = INT_MAX;
dp[0][0] = 1;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++) {
if (dp[i][j] != INT_MAX && (j + mat[i][j]) < n
&& (dp[i][j] + 1) < dp[i][j + mat[i][j]])
dp[i][j + mat[i][j]] = dp[i][j] + 1;
if (dp[i][j] != INT_MAX && (i + mat[i][j]) < m
&& (dp[i][j] + 1) < dp[i + mat[i][j]][j])
dp[i + mat[i][j]][j] = dp[i][j] + 1;
}
}
if (dp[m - 1][n - 1] != INT_MAX)
return dp[m - 1][n - 1];
return -1;
}
int main()
{
int mat[SIZE][SIZE] = { { 2, 3, 2, 1, 4 },
{ 3, 2, 5, 8, 2 },
{ 1, 1, 2, 2, 1 } };
int m = 3, n = 5;
cout << "Minimum number of cells = "
<< minCells(mat, m, n);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class MinCellsDestination
{
static final int SIZE= 100 ;
static int minCells( int mat[][], int m, int n)
{
int dp[][] = new int [m][n];
for ( int i = 0 ; i < m; i++)
for ( int j = 0 ; j < n; j++)
dp[i][j] = Integer.MAX_VALUE;
dp[ 0 ][ 0 ] = 1 ;
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < n; j++) {
if (dp[i][j] != Integer.MAX_VALUE &&
(j + mat[i][j]) < n && (dp[i][j] + 1 )
< dp[i][j + mat[i][j]])
dp[i][j + mat[i][j]] = dp[i][j] + 1 ;
if (dp[i][j] != Integer.MAX_VALUE &&
(i + mat[i][j]) < m && (dp[i][j] + 1 )
< dp[i + mat[i][j]][j])
dp[i + mat[i][j]][j] = dp[i][j] + 1 ;
}
}
if (dp[m - 1 ][n - 1 ] != Integer.MAX_VALUE)
return dp[m - 1 ][n - 1 ];
return - 1 ;
}
public static void main(String args[])
{
int mat[][] = { { 2 , 3 , 2 , 1 , 4 },
{ 3 , 2 , 5 , 8 , 2 },
{ 1 , 1 , 2 , 2 , 1 }};
int m = 3 , n = 5 ;
System.out.println( "Minimum number of cells" +
" = " + minCells(mat, m, n));
}
}
|
Python3
SIZE = 100
MAX = 10000000
def minCells( mat, m, n):
dp = [[ MAX for i in range (n)] for i in range (m)]
dp[ 0 ][ 0 ] = 1
for i in range (m):
for j in range (n):
if (dp[i][j] ! = MAX and
(j + mat[i][j]) < n and
(dp[i][j] + 1 ) < dp[i][j + mat[i][j]]):
dp[i][j + mat[i][j]] = dp[i][j] + 1
if (dp[i][j] ! = MAX and (i + mat[i][j]) < m
and (dp[i][j] + 1 ) < dp[i + mat[i][j]][j]):
dp[i + mat[i][j]][j] = dp[i][j] + 1
if (dp[m - 1 ][n - 1 ] ! = MAX ):
return dp[m - 1 ][n - 1 ]
return - 1
mat = [ [ 2 , 3 , 2 , 1 , 4 ],
[ 3 , 2 , 5 , 8 , 2 ],
[ 1 , 1 , 2 , 2 , 1 ]]
m = 3
n = 5
print ( "Minimum number of cells = " ,
minCells(mat, m, n))
|
C#
using System;
class GFG {
static int minCells( int [,]mat, int m, int n)
{
int [,]dp = new int [m,n];
for ( int i = 0; i < m; i++)
for ( int j = 0; j < n; j++)
dp[i,j] = int .MaxValue;
dp[0,0] = 1;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < n; j++) {
if (dp[i,j] != int .MaxValue &&
(j + mat[i,j]) < n && (dp[i,j] + 1)
< dp[i,j + mat[i,j]])
dp[i,j + mat[i,j]] = dp[i,j] + 1;
if (dp[i,j] != int .MaxValue &&
(i + mat[i,j]) < m && (dp[i,j] + 1)
< dp[i + mat[i,j],j])
dp[i + mat[i,j],j] = dp[i,j] + 1;
}
}
if (dp[m - 1,n - 1] != int .MaxValue)
return dp[m - 1,n - 1];
return -1;
}
public static void Main()
{
int [,]mat = { { 2, 3, 2, 1, 4 },
{ 3, 2, 5, 8, 2 },
{ 1, 1, 2, 2, 1 } };
int m = 3, n = 5;
Console.WriteLine( "Minimum number of "
+ "cells = " + minCells(mat, m, n));
}
}
|
PHP
<?php
function minCells( $mat , $m , $n )
{
$dp = array ( array ());
for ( $i = 0; $i < $m ; $i ++)
for ( $j = 0; $j < $n ; $j ++)
$dp [ $i ][ $j ] = PHP_INT_MAX;
$dp [0][0] = 1;
for ( $i = 0; $i < $m ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if ( $dp [ $i ][ $j ] != PHP_INT_MAX and
( $j + $mat [ $i ][ $j ]) < $n
and ( $dp [ $i ][ $j ] + 1) <
$dp [ $i ][ $j + $mat [ $i ][ $j ]])
$dp [ $i ][ $j + $mat [ $i ][ $j ]] =
$dp [ $i ][ $j ] + 1;
if ( $dp [ $i ][ $j ] != PHP_INT_MAX and
( $i + $mat [ $i ][ $j ]) < $m
and ( $dp [ $i ][ $j ] + 1) <
$dp [ $i + $mat [ $i ][ $j ]][ $j ])
$dp [ $i + $mat [ $i ][ $j ]][ $j ] = $dp [ $i ][ $j ] + 1;
}
}
if ( $dp [ $m - 1][ $n - 1] != PHP_INT_MAX)
return $dp [ $m - 1][ $n - 1];
return -1;
}
$mat = array ( array (2, 3, 2, 1, 4),
array (3, 2, 5, 8, 2),
array (1, 1, 2, 2, 1));
$m = 3; $n = 5;
echo "Minimum number of cells = "
, minCells( $mat , $m , $n );
?>
|
Javascript
<script>
let SIZE=100;
function minCells(mat, m, n)
{
let dp = new Array(m);
for ( var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (let i = 0; i < m; i++)
for (let j = 0; j < n; j++)
dp[i][j] = Number.MAX_VALUE;
dp[0][0] = 1;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (dp[i][j] != Number.MAX_VALUE &&
(j + mat[i][j]) < n && (dp[i][j] + 1)
< dp[i][j + mat[i][j]])
dp[i][j + mat[i][j]] = dp[i][j] + 1;
if (dp[i][j] != Number.MAX_VALUE &&
(i + mat[i][j]) < m && (dp[i][j] + 1)
< dp[i + mat[i][j]][j])
dp[i + mat[i][j]][j] = dp[i][j] + 1;
}
}
if (dp[m - 1][n - 1] != Number.MAX_VALUE)
return dp[m - 1][n - 1];
return -1;
}
let mat = [[ 2, 3, 2, 1, 4 ],
[ 3, 2, 5, 8, 2 ],
[ 1, 1, 2, 2, 1 ]];
let m = 3, n = 5;
document.write( "Minimum number of cells" +
" = " + minCells(mat, m, n));
</script>
|
Output
Minimum number of cells = 4
Time Complexity: O(m*n)
Auxiliary Space: O(m*n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Mar, 2023
Like Article
Save Article