Given a matrix arr[][] of dimensions N * M, having elements 0, 1, and 2. There is only one cell with value 1 present in the matrix. The task is to check if it is possible for 1 to reach the bottom right corner before any cell valued 2 or not using the following operations:
- 2 can replicate itself 1 unit in all four directions in 1 unit of time.
- 1 can only move in one direction among all four directions if the element at that position is 0.
Print “Yes” if a cell with value 1 will reach the bottom right corner in less than or equal amount of time than any cell with value 2. Otherwise, print “1″.
Examples:
Input: N = 3, M = 3, arr[][] = {{0, 2, 0}, {0, 1, 0}, {0, 0, 0}}
Output: Yes
Explanation:
1 can move to the bottom right corner in 2 moves and 2 can move to bottom right corner in 3 moves.
Since, cell with value 1 reaches first than the cell with value 2. Therefore, print Yes.
Input: N = 3, M = 3, arr[][] = {{0, 2, 0}, {0, 1, 0}, {0, 2, 0}}
Output: No
Explanation:
1 can move to the bottom right corner in 2 moves and 2 in the last row of the cell can move to bottom right corner in 1 moves.
Since, cell with value 2 reaches first than the cell with value 1. Therefore, print No.
Approach: The idea is to use a Multi-Source BFS. To perform multi-source BFS Traversal, add all the positions of 1 and 2s present in the matrix, in a Deque in the specified order. Perform the BFS on that dequeue by popping out the added positions and adding the adjacent positions that have not yet been visited. Follow the steps below to solve the problem:
- Create a dequeue for multi-source BFS.
- Firstly, add the position having 1 to the front and then add positions having 2 at the back. This is because if 1 and 2 reach the bottom right at the same time then 1 is considered over 2.
- Pop the elements from the front of the dequeue until the dequeue is empty and do the following:
- If the popped position is already visited, proceed to the next position.
- If the position is not visited, check if it’s a bottom-right position or not as well as check if the element in it is 1 or not. If found to be true, then print Yes.
- Otherwise, for all the four directions, insert the current positions in the dequeue.
- Once the above operations are exhausted, if the cell valued 1 is not found to have reached the bottom-right position, then print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool reachesBottom(vector<vector< int > >& a)
{
int n = a.size();
int m = a[0].size();
deque<vector< int > > q;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (a[i][j] == 1) {
q.push_front({ i, j, 1 });
}
else if (a[i][j] == 2) {
q.push_back({ i, j, 2 });
}
a[i][j] = 0;
}
}
int dx[] = { -1, 0, 1, 0 };
int dy[] = { 0, 1, 0, -1 };
while (!q.empty()) {
auto front = q.front();
q.pop_front();
int i = front[0], j = front[1];
int t = front[2];
if (a[i][j])
continue ;
a[i][j] = 1;
if (t == 1 and (i == n - 1
&& j == m - 1)) {
return true ;
}
for ( int d = 0; d < 4; d++) {
int ni = i + dx[d];
int nj = j + dy[d];
if (ni >= 0 and ni < n
and nj >= 0 and nj < m) {
q.push_back({ ni, nj, t });
}
}
}
return false ;
}
int main()
{
vector<vector< int > > matrix{ { 0, 2, 0 },
{ 0, 1, 0 },
{ 0, 2, 0 } };
if (reachesBottom(matrix)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG{
static boolean reachesBottom( int [][] a)
{
int n = a.length;
int m = a[ 0 ].length;
Deque< int []> q = new LinkedList<>();
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
if (a[i][j] == 1 )
{
q.addFirst( new int []{ i, j, 1 });
}
else if (a[i][j] == 2 )
{
q.addLast( new int []{ i, j, 2 });
}
a[i][j] = 0 ;
}
}
int dx[] = { - 1 , 0 , 1 , 0 };
int dy[] = { 0 , 1 , 0 , - 1 };
while (!q.isEmpty())
{
int [] front = q.peekFirst();
q.removeFirst();
int i = front[ 0 ], j = front[ 1 ];
int t = front[ 2 ];
if (a[i][j] == 1 )
continue ;
a[i][j] = 1 ;
if (t == 1 && (i == n - 1 &&
j == m - 1 ))
{
return true ;
}
for ( int d = 0 ; d < 4 ; d++)
{
int ni = i + dx[d];
int nj = j + dy[d];
if (ni >= 0 && ni < n &&
nj >= 0 && nj < m)
{
q.addLast( new int []{ ni, nj, t });
}
}
}
return false ;
}
public static void main (String[] args)
{
int [][] matrix = { { 0 , 2 , 0 },
{ 0 , 1 , 0 },
{ 0 , 2 , 0 } };
if (reachesBottom(matrix))
{
System.out.print( "YES" );
}
else
{
System.out.print( "NO" );
}
}
}
|
Python3
from collections import deque
def reachesBottom(a):
n = len (a)
m = len (a[ 0 ])
q = deque()
for i in range (n):
for j in range (m):
if (a[i][j] = = 1 ):
q.appendleft([i, j, 1 ])
elif (a[i][j] = = 2 ):
q.append([i, j, 2 ])
a[i][j] = 0
dx = [ - 1 , 0 , 1 , 0 ]
dy = [ 0 , 1 , 0 , - 1 ]
while ( len (q) > 0 ):
front = q.popleft()
i = front[ 0 ]
j = front[ 1 ]
t = front[ 2 ]
if (a[i][j]):
continue
a[i][j] = 1
if (t = = 1 and (i = = n - 1 and
j = = m - 1 )):
return True
for d in range ( 4 ):
ni = i + dx[d]
nj = j + dy[d]
if (ni > = 0 and ni < n and
nj > = 0 and nj < m):
q.append([ni, nj, t])
return False
if __name__ = = '__main__' :
matrix = [ [ 0 , 2 , 0 ],
[ 0 , 1 , 0 ],
[ 0 , 2 , 0 ] ]
if (reachesBottom(matrix)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool reachesBottom( int [,]a,
int n, int m)
{
Queue< int []> q = new Queue< int []>();
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (a[i, j] == 1)
{
q.Enqueue( new int []{i, j, 1});
}
else if (a[i, j] == 2)
{
q.Enqueue( new int []{i, j, 2});
}
a[i, j] = 0;
}
}
int []dx = {-1, 0, 1, 0};
int []dy = {0, 1, 0, -1};
while (q.Count != 0)
{
int [] front = q.Peek();
q.Dequeue();
int i = front[0], j = front[1];
int t = front[2];
if (a[i, j] == 1)
continue ;
a[i, j] = 1;
if (t == 1 && (i == n - 1 &&
j == m - 1))
{
return true ;
}
for ( int d = 0; d < 4; d++)
{
int ni = i + dx[d];
int nj = j + dy[d];
if (ni >= 0 && ni < n &&
nj >= 0 && nj < m)
{
q.Enqueue( new int []{ni, nj, t});
}
}
}
return false ;
}
public static void Main(String[] args)
{
int [,] matrix = {{0, 2, 0},
{0, 1, 0},
{0, 2, 0}};
if (reachesBottom(matrix, 3, 3))
{
Console.Write( "YES" );
}
else
{
Console.Write( "NO" );
}
}
}
|
Javascript
<script>
function reachesBottom(a)
{
var n = a.length;
var m = a[0].length;
var q = [];
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < m; j++) {
if (a[i][j] == 1) {
q.slice(0,0,[i, j, 1]);
}
else if (a[i][j] == 2) {
q.push([i, j, 2 ]);
}
a[i][j] = 0;
}
}
var dx = [-1, 0, 1, 0 ];
var dy = [ 0, 1, 0, -1 ];
while (!q.length) {
var front = q[0];
q.shift();
var i = front[0], j = front[1];
var t = front[2];
if (a[i][j])
continue ;
a[i][j] = 1;
if (t == 1 && (i == n - 1
&& j == m - 1)) {
return true ;
}
for ( var d = 0; d < 4; d++) {
var ni = i + dx[d];
var nj = j + dy[d];
if (ni >= 0 && ni < n
&& nj >= 0 && nj < m) {
q.push([ ni, nj, t ]);
}
}
}
return false ;
}
var matrix = [[ 0, 2, 0 ],
[ 0, 1, 0 ],
[0, 2, 0 ]];
if (reachesBottom(matrix)) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(N*M), The time complexity is O(N*M), where N is the number of rows and M is the number of columns in the given matrix. This is because the program traverses every cell of the matrix once to initialize the deque and again during the BFS traversal. In the worst case, all cells of the matrix may be traversed, resulting in O(N*M) time complexity.
Auxiliary Space: O(N*M), The space complexity of the program is also O(N*M). This is because the program initializes a deque to store all the possible directions of the current cell. In the worst case, the deque may store all the cells of the matrix, resulting in O(N*M) space complexity. Additionally, the program also initializes a 2D vector to represent the matrix, which also takes O(N*M) space. Therefore, the overall space complexity of the program is O(N*M).