Print the indices for every row of a grid from which escaping from the grid is possible
Last Updated :
17 May, 2021
Given a binary 2D array arr[] of dimension M * N representing a grid where ‘0‘ represents that there is a wall on the main diagonal of the cell and ‘1‘ represents that there is a wall on cross diagonal of the cell. The task is for every ith row is to print the index of the row from which one can escape the grid, given that one cannot escape from the top or bottom of the grid.
Examples:
Input: arr[][] = {{1, 1, 0, 1}, {1, 1, 0, 0}, {1, 0, 0, 0}, {1, 1, 0, 1}, {0, 1, 0, 1}}
Output: -1 -1 2 3 -1
Explanation:
No escape path exists from the rows 0, 1 or 4.
If a person enters the grid from the 2nd row, then he will come out of the grid from the cell (2, 3) following the path shown in the diagram above.
If a person enters the grid from the 3rd row, then he will come out of the grid from the cell (3, 3) following the path shown in the diagram above.
Input: arr[][] = {{1, 1, 0}, {0, 1, 0}, {0, 1, 0}, {0, 1, 0}}
Output: -1 2 3 -1
Approach: The given problem can be solved based on the following observations:
- From the above image, it can be observed that for a given orientation of wall depending upon the direction in which a person enters a cell there is only one choice for exiting that cell.
- Therefore, for each row, the idea is to iterate in the given direction keeping track of the direction in which a person enters the cell.
Follow the steps below to solve the problem:
- Iterate over the range [0, M – 1] using a variable row and perform the following operations:
- Initialize two variables say i and j to store the row index and column index of the cell in which the person is currently in.
- Assign row to i and 0 to j.
- Initialize a variable, say dir, to store the direction in which a person enters a cell.
- Iterate until j is not at least N and perform the following operations:
- If arr[i][j] is 1 then check the following conditions:
- If dir is equal to ‘L‘ then decrement i by 1 and assign ‘D‘ to dir.
- Otherwise, if dir is equal to ‘U‘ then decrement j by 1 and assign ‘R‘ to dir.
- Otherwise, if dir is equal to ‘R‘ then increment i by 1 and assign ‘U‘ to dir.
- Otherwise, increment j by 1 and assign ‘L‘ to dir.
- Otherwise, if arr[i][j] is 0 then check the following conditions:
- If dir is equal to ‘L‘ then increment i by 1 and assign ‘U‘ to dir.
- Otherwise, if dir is equal to ‘U‘ then increment j by 1 and assign ‘L‘ to dir.
- Otherwise, if dir is equal to ‘R‘ then decrement i by 1 and assign ‘D‘ to dir.
- Otherwise, decrement j by 1 and assign ‘R‘ to dir.
- Otherwise, if i or j is 0 or i is equal to M or j is equal to N then break.
- If j is equal to N then print the value i.
- Otherwise, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPath(vector<vector< int > >& arr,
int M, int N)
{
for ( int row = 0; row < M; row++) {
char dir = 'L' ;
int i = row;
int j = 0;
while (j < N) {
if (arr[i][j] == 1) {
if (dir == 'L' ) {
i--;
dir = 'D' ;
}
else if (dir == 'U' ) {
j--;
dir = 'R' ;
}
else if (dir == 'R' ) {
i++;
dir = 'U' ;
}
else if (dir == 'D' ) {
j++;
dir = 'L' ;
}
}
else {
if (dir == 'L' ) {
i++;
dir = 'U' ;
}
else if (dir == 'U' ) {
j++;
dir = 'L' ;
}
else if (dir == 'R' ) {
i--;
dir = 'D' ;
}
else if (dir == 'D' ) {
j--;
dir = 'R' ;
}
}
if (i < 0 || i == M || j < 0 || j == N)
break ;
}
if (j == N)
cout << i << " " ;
else
cout << -1 << " " ;
}
}
int main()
{
vector<vector< int > > arr = { { 1, 1, 0, 1 },
{ 1, 1, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
int M = arr.size();
int N = arr[0].size();
findPath(arr, M, N);
}
|
Java
import java.util.*;
class GFG{
static void findPath( int [][]arr,
int M, int N)
{
for ( int row = 0 ; row < M; row++)
{
char dir = 'L' ;
int i = row;
int j = 0 ;
while (j < N)
{
if (arr[i][j] == 1 )
{
if (dir == 'L' )
{
i--;
dir = 'D' ;
}
else if (dir == 'U' )
{
j--;
dir = 'R' ;
}
else if (dir == 'R' )
{
i++;
dir = 'U' ;
}
else if (dir == 'D' )
{
j++;
dir = 'L' ;
}
}
else
{
if (dir == 'L' )
{
i++;
dir = 'U' ;
}
else if (dir == 'U' )
{
j++;
dir = 'L' ;
}
else if (dir == 'R' )
{
i--;
dir = 'D' ;
}
else if (dir == 'D' )
{
j--;
dir = 'R' ;
}
}
if (i < 0 || i == M || j < 0 || j == N)
break ;
}
if (j == N)
System.out.print(i + " " );
else
System.out.print(- 1 + " " );
}
}
public static void main(String[] args)
{
int [][]arr = { { 1 , 1 , 0 , 1 },
{ 1 , 1 , 0 , 0 },
{ 1 , 0 , 0 , 0 },
{ 1 , 1 , 0 , 1 },
{ 0 , 1 , 0 , 1 } };
int M = arr.length;
int N = arr[ 0 ].length;
findPath(arr, M, N);
}
}
|
Python3
def findPath(arr, M, N) :
for row in range (M):
dir = 'L'
i = row
j = 0
while (j < N) :
if (arr[i][j] = = 1 ) :
if ( dir = = 'L' ) :
i - = 1
dir = 'D'
elif ( dir = = 'U' ) :
j - = 1
dir = 'R'
elif ( dir = = 'R' ) :
i + = 1
dir = 'U'
elif ( dir = = 'D' ) :
j + = 1
dir = 'L'
else :
if ( dir = = 'L' ) :
i + = 1
dir = 'U'
elif ( dir = = 'U' ) :
j + = 1
dir = 'L'
elif ( dir = = 'R' ) :
i - = 1
dir = 'D'
elif ( dir = = 'D' ) :
j - = 1
dir = 'R'
if (i < 0 or i = = M or j < 0 or j = = N):
break
if (j = = N) :
print (i, end = " " )
else :
print ( - 1 , end = " " )
arr = [[ 1 , 1 , 0 , 1 ],
[ 1 , 1 , 0 , 0 ],
[ 1 , 0 , 0 , 0 ],
[ 1 , 1 , 0 , 1 ],
[ 0 , 1 , 0 , 1 ]]
M = len (arr)
N = len (arr[ 0 ])
findPath(arr, M, N)
|
C#
using System;
class GFG {
static void findPath( int [, ] arr, int M, int N)
{
for ( int row = 0; row < M; row++) {
char dir = 'L' ;
int i = row;
int j = 0;
while (j < N) {
if (arr[i, j] == 1) {
if (dir == 'L' ) {
i--;
dir = 'D' ;
}
else if (dir == 'U' ) {
j--;
dir = 'R' ;
}
else if (dir == 'R' ) {
i++;
dir = 'U' ;
}
else if (dir == 'D' ) {
j++;
dir = 'L' ;
}
}
else {
if (dir == 'L' ) {
i++;
dir = 'U' ;
}
else if (dir == 'U' ) {
j++;
dir = 'L' ;
}
else if (dir == 'R' ) {
i--;
dir = 'D' ;
}
else if (dir == 'D' ) {
j--;
dir = 'R' ;
}
}
if (i < 0 || i == M || j < 0 || j == N)
break ;
}
if (j == N)
Console.Write(i + " " );
else
Console.Write(-1 + " " );
}
}
public static void Main()
{
int [, ] arr = { { 1, 1, 0, 1 },
{ 1, 1, 0, 0 },
{ 1, 0, 0, 0 },
{ 1, 1, 0, 1 },
{ 0, 1, 0, 1 } };
int M = arr.GetLength(0);
int N = arr.GetLength(1);
findPath(arr, M, N);
}
}
|
Javascript
<script>
function findPath(arr, M, N)
{
for (let row = 0; row < M; row++)
{
let dir = 'L' ;
let i = row;
let j = 0;
while (j < N)
{
if (arr[i][j] == 1)
{
if (dir == 'L' )
{
i--;
dir = 'D' ;
}
else if (dir == 'U' )
{
j--;
dir = 'R' ;
}
else if (dir == 'R' )
{
i++;
dir = 'U' ;
}
else if (dir == 'D' )
{
j++;
dir = 'L' ;
}
}
else
{
if (dir == 'L' )
{
i++;
dir = 'U' ;
}
else if (dir == 'U' )
{
j++;
dir = 'L' ;
}
else if (dir == 'R' )
{
i--;
dir = 'D' ;
}
else if (dir == 'D' )
{
j--;
dir = 'R' ;
}
}
if (i < 0 || i == M || j < 0 || j == N)
break ;
}
if (j == N)
document.write(i + " " );
else
document.write(-1 + " " );
}
}
let arr = [[ 1, 1, 0, 1 ],
[ 1, 1, 0, 0 ],
[ 1, 0, 0, 0 ],
[ 1, 1, 0, 1 ],
[ 0, 1, 0, 1 ]];
let M = arr.length;
let N = arr[0].length;
findPath(arr, M, N);
</script>
|
Time Complexity: O(M * N).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...