C++ code to find out if a grid is fully accessible



Suppose, we are given a grid that has 2 rows and n columns. A robot is at position (0, 0) in the grid and wants to visit (1, n - 1) by visiting adjacent and corner cells to its current location. We are given the grid in an array of strings, a cell is blocked if it is marked '#' and it is accessible if it is marked '.'. We have to find out if the robot can visit cell (1, n - 1) from the cell (0, 0).

So, if the input is like n = 4, grid = {".##.", "...."}, then the output will be Possible.

Steps

To solve this, we will follow these steps −

flag := 1 for initialize i := 0, when i < n, update (increase i by 1), do:    if grid[0, i] is same as '#' and grid[1, i] is same as '#', then:    flag := 0 if flag is same as 0, then:    print("Not Possible.") Otherwise    print("Possible.")

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, string grid[]) {    int flag = 1;    for(int i = 0; i < n; i++){       if(grid[0].at(i) == '#' && grid[1].at(i) == '#'){          flag = 0;       }    }    if (flag == 0)       cout<<"Not Possible.";    else       cout<<"Possible."; } int main() {    int n = 4;    string grid[] = {".##.", "...."};    solve(n, grid);    return 0; }

Input

4, {".##.", "...."}

Output

Possible.
Updated on: 2022-03-29T08:36:58+05:30

240 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements