Queries to check if traversal is possible from (1,1) to (2, N)
Last Updated :
28 Feb, 2023
Given a grid of size, 2 rows * N columns and array Q[][2] of size M, the task for this problem is to check traversal from (1, 1) to (2, N) is possible or not in the given grid for each query given X and Y, block cell (X, Y) if it is open or open cell (X, Y) if it is blocked. Initially, all cells are open for traversal.
Note: Open cells allow traversal through that cell and blocked cell does not allow traversal through that cell.
Examples:
Input: N = 5, Q[][2] = {{2, 3}, {1, 4}, {2, 4}, {2, 3}, {1, 4}}
Output: Yes
No
No
No
Yes
Explanation: Initially all cells are open for traversal
- query 1 : (2, 3) was open for traversal and now after performing first query it is blocked. Traversal from (1, 1) to (2, N) is still possible.
- query 2 : (1, 4) was open for traversal and now after performing second query it is blocked. Traversal from (1, 1) to (2, N) is not possible.
- query 3 : (2, 4) was open for traversal and now after performing third query it is blocked. Traversal from (1, 1) to (2, N) is not possible.
- query 4 : (2, 3) was blocked for traversal and now after performing fourth query it is open. Traversal from (1, 1) to (2, N) is not possible.
- query 5 : (1, 4) was blocked for traversal and now after performing fifth query it is open. Traversal from (1, 1) to (2, N) is possible.
Input: int N = 2, Q[][2] = {{2, 1}, {1, 2}, {1, 2}, {1, 2}};
Output: Yes
No
Yes
No
Naive approach: The basic way to solve the problem is as follows:
The basic way to solve this problem is to update grid for each query and with DFS (Depth First Search) check if traversal from (1, 1) to (2, N) is possible or not for each query.
Time Complexity: O(N * Q)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized based on the following idea:
- Greedy algorithms can be used to solve this problem.
- Count of Blocks can be tracked and if blocks are zero then answer will be “Yes” else “No”.
Follow the steps below to solve the problem:
- 2d array grid[2][N] is created initially with all values zero.
- variable CNT is created for tracking the number of blocks initially with a value of zero.
- Iterating for each M query and for each query updating grid[2][N].
- if CNT is zero then print “Yes” or else “No” for each query.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countWays( int N, int Q[][2], int M)
{
vector<vector< int > > grid(2, vector< int >(N, 0));
int cnt = 0;
for ( int i = 0; i < M; i++) {
int X = Q[i][0] - 1, Y = Q[i][1] - 1;
if (grid[X][Y]) {
grid[X][Y] = 0;
if (Y == 0) {
cnt = cnt - grid[X - 1][Y + 1];
}
else if (Y == N - 1) {
cnt = cnt - grid[X + 1][Y - 1];
}
else {
cnt = cnt - grid[X ^ 1][Y - 1];
cnt = cnt - grid[X ^ 1][Y];
cnt = cnt - grid[X ^ 1][Y + 1];
}
}
else {
grid[X][Y] = 1;
if (Y == 0) {
cnt = cnt + grid[X - 1][Y + 1];
}
else if (Y == N - 1) {
cnt = cnt + grid[X + 1][Y - 1];
}
else {
cnt = cnt + grid[X ^ 1][Y - 1];
cnt = cnt + grid[X ^ 1][Y];
cnt = cnt + grid[X ^ 1][Y + 1];
}
}
if (cnt == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
int main()
{
int N = 5, Q[][2] = {
{ 2, 3 }, { 1, 4 }, { 2, 4 }, { 2, 3 }, { 1, 4 }
};
int M = 5;
countWays(N, Q, M);
cout << endl;
int N1 = 2,
Q1[][2]
= { { 2, 1 }, { 1, 2 }, { 1, 2 }, { 1, 2 } };
int M1 = 4;
countWays(N1, Q1, M1);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int N, M;
static int cnt;
static int [][] grid, Q;
static void countWays( int N, int [][] Q, int M)
{
for ( int i = 0 ; i < M; i++) {
int X = Q[i][ 0 ] - 1 , Y = Q[i][ 1 ] - 1 ;
if (grid[X][Y] == 1 ) {
grid[X][Y] = 0 ;
if (Y == 0 ) {
cnt -= grid[X - 1 ][Y + 1 ];
}
else if (Y == N - 1 ) {
cnt -= grid[X + 1 ][Y - 1 ];
}
else {
cnt -= grid[X ^ 1 ][Y - 1 ];
cnt -= grid[X ^ 1 ][Y];
cnt -= grid[X ^ 1 ][Y + 1 ];
}
}
else {
grid[X][Y] = 1 ;
if (Y == 0 ) {
cnt += grid[X - 1 ][Y + 1 ];
}
else if (Y == N - 1 ) {
cnt += grid[X + 1 ][Y - 1 ];
}
else {
cnt += grid[X ^ 1 ][Y - 1 ];
cnt += grid[X ^ 1 ][Y];
cnt += grid[X ^ 1 ][Y + 1 ];
}
}
if (cnt == 0 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
public static void main(String[] args)
{
N = 5 ;
Q = new int [][] {
{ 2 , 3 }, { 1 , 4 }, { 2 , 4 }, { 2 , 3 }, { 1 , 4 }
};
M = 5 ;
grid = new int [ 2 ][N];
countWays(N, Q, M);
System.out.println();
N = 2 ;
Q = new int [][] {
{ 2 , 1 }, { 1 , 2 }, { 1 , 2 }, { 1 , 2 }
};
M = 4 ;
grid = new int [ 2 ][N];
countWays(N, Q, M);
}
}
|
Python3
def countWays(N, Q, M):
grid = [[ 0 for j in range (N)] for i in range ( 2 )]
cnt = 0
for i in range (M):
X, Y = Q[i][ 0 ] - 1 , Q[i][ 1 ] - 1
if grid[X][Y]:
grid[X][Y] = 0
if Y = = 0 :
cnt - = grid[X - 1 ][Y + 1 ]
elif Y = = N - 1 :
cnt - = grid[X + 1 ][Y - 1 ]
else :
cnt - = grid[X ^ 1 ][Y - 1 ]
cnt - = grid[X ^ 1 ][Y]
cnt - = grid[X ^ 1 ][Y + 1 ]
else :
grid[X][Y] = 1
if Y = = 0 :
cnt + = grid[X - 1 ][Y + 1 ]
elif Y = = N - 1 :
cnt + = grid[X + 1 ][Y - 1 ]
else :
cnt + = grid[X ^ 1 ][Y - 1 ]
cnt + = grid[X ^ 1 ][Y]
cnt + = grid[X ^ 1 ][Y + 1 ]
if cnt = = 0 :
print ( "Yes" )
else :
print ( "No" )
if __name__ = = "__main__" :
N = 5
Q = [[ 2 , 3 ], [ 1 , 4 ], [ 2 , 4 ], [ 2 , 3 ], [ 1 , 4 ]]
M = 5
countWays(N, Q, M)
print ()
N1 = 2
Q1 = [[ 2 , 1 ], [ 1 , 2 ], [ 1 , 2 ], [ 1 , 2 ]]
M1 = 4
countWays(N1, Q1, M1)
|
C#
using System;
public class GFG {
static int N, M;
static int cnt;
static int [, ] grid, Q;
static void countWays( int N, int [, ] Q, int M)
{
for ( int i = 0; i < M; i++) {
int X = Q[i, 0] - 1, Y = Q[i, 1] - 1;
if (grid[X, Y] == 1) {
grid[X, Y] = 0;
if (Y == 0) {
cnt -= grid[X - 1, Y + 1];
}
else if (Y == N - 1) {
cnt -= grid[X + 1, Y - 1];
}
else {
cnt -= grid[X ^ 1, Y - 1];
cnt -= grid[X ^ 1, Y];
cnt -= grid[X ^ 1, Y + 1];
}
}
else {
grid[X, Y] = 1;
if (Y == 0) {
cnt += grid[X - 1, Y + 1];
}
else if (Y == N - 1) {
cnt += grid[X + 1, Y - 1];
}
else {
cnt += grid[X ^ 1, Y - 1];
cnt += grid[X ^ 1, Y];
cnt += grid[X ^ 1, Y + 1];
}
}
if (cnt == 0) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
static public void Main()
{
N = 5;
Q = new int [, ] {
{ 2, 3 }, { 1, 4 }, { 2, 4 }, { 2, 3 }, { 1, 4 }
};
M = 5;
grid = new int [2, N];
countWays(N, Q, M);
Console.WriteLine();
N = 2;
Q = new int [, ] {
{ 2, 1 }, { 1, 2 }, { 1, 2 }, { 1, 2 }
};
M = 4;
grid = new int [2, N];
countWays(N, Q, M);
}
}
|
Javascript
function countWays(N, Q, M) {
var grid = new Array(2);
for ( var i = 0; i < 2; i++) {
grid[i] = new Array(N).fill(0);
}
var cnt = 0;
for ( var i = 0; i < M; i++) {
var X = Q[i][0] - 1;
var Y = Q[i][1] - 1;
if (grid[X][Y]) {
grid[X][Y] = 0;
if (Y == 0) {
cnt -= grid[X - 1][Y + 1];
}
else if (Y == N - 1) {
cnt -= grid[X + 1][Y - 1];
}
else {
cnt -= grid[X ^ 1][Y - 1];
cnt -= grid[X ^ 1][Y];
cnt -= grid[X ^ 1][Y + 1];
}
}
else {
grid[X][Y] = 1;
if (Y == 0) {
cnt += grid[X - 1][Y + 1];
}
else if (Y == N - 1) {
cnt += grid[X + 1][Y - 1];
}
else {
cnt += grid[X ^ 1][Y - 1];
cnt += grid[X ^ 1][Y];
cnt += grid[X ^ 1][Y + 1];
}
}
if (cnt == 0) {
console.log( "Yes" );
}
else {
console.log( "No" );
}
}
}
if ( true ) {
var N = 5;
var Q = [[2, 3], [1, 4], [2, 4], [2, 3], [1, 4]];
var M = 5;
countWays(N, Q, M);
console.log();
var N1 = 2;
var Q1 = [[2, 1], [1, 2], [1, 2], [1, 2]];
var M1 = 4;
countWays(N1, Q1, M1);
}
|
Output
Yes
No
No
No
Yes
Yes
No
Yes
No
Time Complexity: O(N + Q)
Auxiliary Space: O(N)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...