Labyrinth-II

You are playing a computer game where you are dropped into a maze represented by a 2D character array grid. The grid consists of empty spaces ' . ' and obstacles ' * '. You start at the cell located at row r and column c (0-indexed).

In one step, you can move up, down, left, or right to any adjacent empty cell. You cannot step outside the boundaries of the labyrinth.

Unfortunately, your keyboard is breaking: you can move left no more than maxLeft times and move right no more than maxRight times. There are no restrictions on the number of up and down moves, as those keys are in perfect condition.

Return the total number of cells in the labyrinth that you can reach, including the starting cell itself.

Example 1:

Input: grid = [

['.', '.'],

['.', '*']

], r = 0, c = 0, maxLeft = 0, maxRight = 1

Output: 3

Explanation: - Start at (0,0).

  • Move Right to (0,1) (Uses 1 Right move).
  • Move Down to (1,0) (Uses 0 Left/Right moves).
  • You cannot reach (1,1) because it is an obstacle *. Total reachable cells: 3.

Example 2:

Input: grid = [

['*', '*', '*'],

['*', '.', '*'],

['*', '*', '*']

], r = 1, c = 1, maxLeft = 100, maxRight = 100

Output: 1

Explanation:

The starting cell is completely surrounded by obstacles. Only the starting cell itself is reachable. Total reachable cells: 1.

Now Your Turn!

Pick the correct output for the given input

Input: grid = [

['.', '.', '.'],

['*', '*', '*']

], r = 0, c = 1, maxLeft = 0, maxRight = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= grid.length <= 2000
  • 1 <= grid[i].length <= 2000
  • 0 <= r < grid.length
  • 0 <= c < grid[i].length
  • 0 <= maxLeft, maxRight <= 109
  • grid[i][j] is either '.' or '*'.
  • grid[r][c] == '.' (The starting cell is guaranteed to be empty).

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int reachableCells(vector<vector<char>>& grid, int r, int c, int maxLeft, int maxRight) {
// Write your code here
}
};
Test Case

Input:

C
Grid
Max Left
Max Right
R