Geek in a Maze
Geek is in a maze of size N * M. Each cell in the maze is made of either ‘.’ or ‘#’. An empty cell is represented by ‘.’ and an obstacle is represented by ‘#’. The task is to find out how many different empty cells he can pass through If Geek starts at cell (R, C) and avoids the obstacles and he can move in any of the four directions but he can move up at most U times and he can move down at most D times.
Examples:
Input: N = 3, M = 3,
R = 1, C = 0
U = 1, D = 1
mat = {{. . .}, {. # .}, {# . .}}
Output: 5
Explanation: Geek can reach
(1, 0), (0, 0), (0, 1), (0, 2), (1, 2)Input: N = 3, M = 4, R = 1, C = 0, U = 1, D = 2
mat = {{. . .}, {. # .}, {. . .}, {# . .}}
Output: 10
Explanation: Geek can reach all the
cells except for the obstacles.
Approach: The idea is to solve this problem is based on the following idea:
Keep moving radially in all four directions (up, down, left, right) and keep counting the number of turning taken in moving up and down. If the number of turns left for given upward and downward movements are not 0 then move to up and down and keep counting the empty cells.
Follow the steps mentioned below to implement the idea:
- Check if the starting point is blocked by an obstacle (#)
- If true, return 0.
- Keep a queue of arrays to store rows, columns, ups, and downs for any cell.
- Do a BFS traversal:
- Check if the cell is empty then increment the count variable (say cnt).
- Check if any up move is left or not.
- If moves for up is left then move to up and decrement the move count for up and push the current status of the cell in the queue.
- Check if any down move is left or not.
- If moves for down is left then move to down and decrement the move count for down and push the current status of the cell in the queue.
- Finally, return the cnt.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to count different empty cells // he can pass through while avoiding // the obstacles int numberOfCells( int n, int m, int r, int c, int u, int d, vector<vector< char > >& mat) { // If cell having Obstacle if (mat[r] == '#' ) return 0; queue<vector< int > > que; int cnt = 0; int i = 0; int j = 0; mat[r] = '#' ; que.push({ r, c, u, d }); // BFS traversal of the matrix while (que.size()) { auto & f = que.front(); int rr = f[0]; int cc = f[1]; int uu = f[2]; int dd = f[3]; que.pop(); ++cnt; // Move left i = rr; j = cc - 1; if (0 <= j && mat[i][j] == '.' ) { // Mark the cell visited mat[i][j] = '#' ; que.push({ i, j, uu, dd }); } // Move right i = rr; j = cc + 1; if (j < m && mat[i][j] == '.' ) { // Mark the cell visited mat[i][j] = '#' ; que.push({ i, j, uu, dd }); } // Move up i = rr - 1; j = cc; if (0 <= i && mat[i][j] == '.' && uu) { // Mark the cell visited mat[i][j] = '#' ; que.push({ i, j, uu - 1, dd }); } // Move down i = rr + 1; j = cc; if (i < n && mat[i][j] == '.' && dd) { // Mark the cell visited mat[i][j] = '#' ; que.push({ i, j, uu, dd - 1 }); } } // Return the count return cnt; } // Driver code int main() { int N = 3, M = 3, R = 1, C = 0; int U = 1, D = 1; vector<vector< char > > mat = { { '.' , '.' , '.' }, { '.' , '#' , '.' }, { '#' , '.' , '.' } }; // Function call cout << numberOfCells(N, M, R, C, U, D, mat); return 0; } |
Java
import java.util.*; import java.io.*; // Java program for the above approach class GFG{ // Function to count different empty cells // he can pass through while avoiding // the obstacles public static int numberOfCells( int n, int m, int r, int c, int u, int d, ArrayList<ArrayList<Character>> mat) { // If cell having Obstacle if (mat.get(r).get(c) == '#' ) return 0 ; Queue<ArrayList<Integer>> que = new ArrayDeque<ArrayList<Integer>>(); int cnt = 0 ; int i = 0 ; int j = 0 ; mat.get(r).set(c, '#' ); que.add( new ArrayList<Integer>(List.of(r, c, u, d))); // BFS traversal of the matrix while (!que.isEmpty()) { ArrayList<Integer> f = que.peek(); int rr = f.get( 0 ); int cc = f.get( 1 ); int uu = f.get( 2 ); int dd = f.get( 3 ); que.remove(); ++cnt; // Move left i = rr; j = cc - 1 ; if ( 0 <= j && mat.get(i).get(j) == '.' ) { // Mark the cell visited mat.get(i).set(j, '#' ); que.add( new ArrayList<Integer>(List.of(i, j, uu, dd))); } // Move right i = rr; j = cc + 1 ; if (j < m && mat.get(i).get(j) == '.' ) { // Mark the cell visited mat.get(i).set(j, '#' ); que.add( new ArrayList<Integer>(List.of(i, j, uu, dd))); } // Move up i = rr - 1 ; j = cc; if ( 0 <= i && mat.get(i).get(j) == '.' && uu > 0 ) { // Mark the cell visited mat.get(i).set(j, '#' ); que.add( new ArrayList<Integer>(List.of(i, j, uu- 1 , dd))); } // Move down i = rr + 1 ; j = cc; if (i < n && mat.get(i).get(j) == '.' && dd > 0 ) { // Mark the cell visited mat.get(i).set(j, '#' ); que.add( new ArrayList<Integer>(List.of(i, j, uu, dd- 1 ))); } } // Return the count return cnt; } // Driver code public static void main(String args[]) { int N = 3 , M = 3 , R = 1 , C = 0 ; int U = 1 , D = 1 ; ArrayList<ArrayList<Character>> mat = new ArrayList<ArrayList<Character>>( List.of( new ArrayList<Character>( List.of( '.' , '.' , '.' ) ), new ArrayList<Character>( List.of( '.' , '#' , '.' ) ), new ArrayList<Character>( List.of( '#' , '.' , '.' ) )) ); // Function call System.out.println(numberOfCells(N, M, R, C, U, D, mat)); } } // This code is contributed by subhamgoyal2014. |
5
Time Complexity: O(N * M)
Auxiliary Space: O(N * M)