337. Robot Room Cleaner
You are given control of a robot placed inside a room represented by an m x n binary grid:
- 0 represents a blocked cell (wall)
- 1 represents an empty cell
The robot starts at an unknown empty cell, and you do not have direct access to the room layout.
You can only interact with the robot using the following API:
interface Robot {
// Returns true if the cell in front is open and the robot moves forward.
// Returns false if the cell in front is blocked and the robot stays in place.
boolean move();
// Rotates the robot 90 degrees to the left.
void turnLeft();
// Rotates the robot 90 degrees to the right.
void turnRight();
// Cleans the current cell.
void clean();
}
The robot is initially facing up.
Your task is to design an algorithm that makes the robot clean every reachable empty cell in the room.
You must solve the problem without knowing the robot’s exact starting position or the room layout.
In other words, your solution must work using only the given API methods.
It is guaranteed that the room is surrounded by walls on all four boundaries.
Example 1:
Input: room = [[1,1,1],[1,1,1],[1,1,1]], row = 1, col = 1
Output: Robot cleaned all rooms.
Explanation:
The robot starts at the center cell of a 3 x 3 room with no obstacles.
Since every cell is empty and reachable, the robot must clean all 9 cells.
Example 2:
Input: room = [[1,0,1],[1,1,1],[0,1,0]], row = 1, col = 1
Output: Robot cleaned all rooms.
Explanation:
The robot starts from the middle cell.
Some neighboring cells are blocked, so the robot must carefully explore only valid paths.
All empty cells in this room are reachable from the starting position and must be cleaned.
Now Your Turn!
Pick the correct output for the given inputInput: room = [[1,1],[1,1]], row = 0, col = 0Input: room = [[1,1,0],[0,1,0],[0,1,1]], row = 0, col = 0
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- m == room.length
- n == room[i].length
- 1 <= m <= 100
- 1 <= n <= 200
- room[i][j] is either 0 or 1
- 0 <= row < m
- 0 <= col < n
- room[row][col] == 1
- All empty cells are reachable from the starting position