FLIPKART Coding Question – Solved

5 Live
You are given a board of size M × N where each cell can be either empty ('O') or a wall ('X'). A robot is initially placed at the top-left cell (0,0) and can move in four directions: up, down, left, and right — but not diagonally. The robot can only move through empty cells and cannot pass through or land on walls. Your task is to print the maximum number of empty cells the robot can visit starting from the top-left cell, including the starting cell. Input Format: - First line: Two integers M and N — the number of rows and columns of the board. - Next M lines: Each contains a string of length N, consisting of characters 'O' and 'X'. Output Format: - A single integer — the number of empty cells the robot can visit. Constraints: - 1 ≤ M, N ≤ 100 Sample Input 1: 4 3 OXO OOX XOO OOO Sample Output 1: 8 Explanation: Starting at (0,0), the robot can move through a connected component of 8 'O' cells. One cell ('O' at (0,2)) is isolated and cannot be reached. Sample Input 2: 4 3 000 00X OXO X00 Sample Output 2: 6 Explanation: Starting at (0,0), the robot can visit 6 cells before being blocked by walls.

Asked in: FLIPKART

Image of the Question

Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <bits/stdc++.h>
using namespace std;

int main() {
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve this problem, think about how to explore the board starting from the initial cell (0,0) and count how many reachable empty cells ('O') the robot can visit. The challenge is to navigate only through empty cells, avoid walls ('X'), and move only up, down, left, or right.

Step 1: Understand the Problem Constraints and Setup
You are given a grid (M rows by N columns). Each cell is either empty ('O') or blocked by a wall ('X'). The robot starts at the top-left corner (0,0), which is guaranteed to be within the grid. The robot can move in four directions but cannot move diagonally or pass through walls. The goal is to find the size of the largest reachable area of empty cells starting from (0,0), including the starting cell.

Step 2: Initial Validation
First, check if the starting cell (0,0) is empty. If it is a wall, the robot cannot move anywhere, so the answer is zero. If it is empty, you will proceed to explore.

Step 3: Modeling the Movement and Reachability
The problem can be thought of as a graph traversal on a grid:
- Each empty cell represents a node.
- Edges exist between nodes if they are adjacent vertically or horizontally (not diagonally) and both are empty cells.
The task reduces to finding the number of nodes in the connected component containing the start node (0,0).

Step 4: Choosing the Traversal Method
Two common traversal strategies are:
- Depth-First Search (DFS)
- Breadth-First Search (BFS)
Either will work efficiently here since the grid is at most 100×100, so up to 10,000 cells.

Step 5: Implementing Traversal
- Maintain a visited matrix (same size as the board) to mark cells already visited to avoid cycles or repeated counting.
- Start from (0,0), mark it visited, and increment count by one.
- For each current cell, explore all four neighbors: up, down, left, right.
- Check boundary conditions to avoid moving outside the grid.
- Check if the neighbor is an empty cell and not yet visited before moving.
- Recursively (for DFS) or iteratively (for BFS) repeat this process for each neighbor.

Step 6: Counting the Reachable Cells
Each time you visit a new empty cell, increment a counter. By the time traversal ends (when no new neighbors can be visited), the counter reflects the number of reachable empty cells from the start.

Step 7: Consider Edge Cases
- Entire board filled with walls except starting cell. The result is 1.
- Starting cell itself is a wall. The result is 0.
- Multiple disconnected clusters of empty cells. Only the cluster containing (0,0) matters since the robot can’t teleport or jump.
- Boards with varying sizes and shapes of open areas.

Step 8: Output
After the traversal completes, print the final count of reachable cells.

Step 9: Summary and Conceptual Overview
This problem is a classic example of flood fill or connected components on a grid. The main idea is:
- Represent the grid as a graph where empty cells are vertices and edges connect adjacent empty cells.
- Use DFS or BFS from the starting vertex to explore the connected component.
- Keep track of visited nodes to avoid infinite loops or redundant counting.
- The size of the connected component visited is the answer.

By thinking of the grid in this way, the problem becomes a manageable graph traversal task rather than a complicated pathfinding challenge. The focus is on exploring the reachable area and counting nodes, not finding a specific path.

Finally, always ensure your traversal respects boundaries and walls, carefully marking visited cells to maintain correctness and efficiency.
```


Related Questions