Robot Room Cleaner - Problem
You control a cleaning robot in an unknown room layout! 🤖 The robot starts at an unknown position in a room represented as a binary grid where
Available Robot API:
The Challenge: You have no map, no coordinates, no sensors - just these four commands! The robot initially faces UP and is guaranteed to start in an empty cell. All room edges are surrounded by walls.
This is a classic "blind exploration" problem that tests your ability to systematically traverse an unknown space using backtracking algorithms.
0 = wall and 1 = empty space. Your mission: clean every accessible cell using only these blind navigation commands:Available Robot API:
move()- Move forward if possible, returns true/falseturnLeft()- Rotate 90° counterclockwiseturnRight()- Rotate 90° clockwiseclean()- Clean current cell
The Challenge: You have no map, no coordinates, no sensors - just these four commands! The robot initially faces UP and is guaranteed to start in an empty cell. All room edges are surrounded by walls.
This is a classic "blind exploration" problem that tests your ability to systematically traverse an unknown space using backtracking algorithms.
Input & Output
example_1.py — Simple 3x3 Room
$
Input:
room = [[1,1,1,1,1,0,1,1],[1,1,1,1,1,0,1,1],[1,0,1,1,1,1,1,1],[0,0,0,1,0,0,0,0],[1,1,1,1,1,1,1,1]], robot starts at (1,3)
›
Output:
Robot cleans all accessible cells: (0,0),(0,1),(0,2),(0,3),(0,4),(0,6),(0,7),(1,0),(1,1),(1,2),(1,3),(1,4),(1,6),(1,7),(2,0),(2,2),(2,3),(2,4),(2,5),(2,6),(2,7),(4,0),(4,1),(4,2),(4,3),(4,4),(4,5),(4,6),(4,7)
💡 Note:
The DFS algorithm systematically explores all reachable cells. Starting from (1,3), it uses virtual coordinates and explores up, right, down, left directions recursively, backtracking when hitting walls or visited cells. All empty cells (marked as 1) get cleaned exactly once.
example_2.py — L-shaped Room
$
Input:
room = [[1,1,0],[1,1,1],[1,0,1]], robot starts at (1,1)
›
Output:
Robot cleans: (0,0),(0,1),(1,0),(1,1),(1,2),(2,0),(2,2)
💡 Note:
In this L-shaped room, the robot starts at the center and systematically explores all connected empty cells. The backtracking ensures it returns to explore all branches of the L-shape, cleaning each accessible cell exactly once.
example_3.py — Single Cell Room
$
Input:
room = [[1]], robot starts at (0,0)
›
Output:
Robot cleans: (0,0)
💡 Note:
Edge case with only one cell. The robot cleans the starting position and finds no valid moves in any direction, completing successfully with 100% coverage of the single accessible cell.
Visualization
Tap to expand
Understanding the Visualization
1
Initialize
Robot starts at unknown position (0,0 in virtual coordinates), creates visited set, begins facing UP
2
Clean Current
Clean current cell and mark coordinates as visited in our tracking set
3
Try All Directions
Systematically try each direction: UP (0°), RIGHT (90°), DOWN (180°), LEFT (270°)
4
Explore or Skip
If cell unvisited and move succeeds → recurse; if wall/visited → turn to next direction
5
Backtrack
After exploring a direction, turn 180°, move back, turn 180° to restore position/orientation
6
Complete
Continue until all directions tried from all reachable positions - guaranteed complete coverage!
Key Takeaway
🎯 Key Insight: Transform the unknown physical space into a logical coordinate system, then use systematic DFS traversal with backtracking to guarantee complete coverage - like a methodical explorer who never gets lost!
Time & Space Complexity
Time Complexity
O(N - M)
Where N is total cells and M is walls. We visit each empty cell exactly once, with constant time backtracking operations
✓ Linear Growth
Space Complexity
O(N - M)
Space for visited set storing all empty cells, plus O(N-M) recursion stack in worst case (straight line room)
✓ Linear Space
Constraints
- m == room.length
- n == room[i].length
- 1 ≤ m, n ≤ 100
- room[i][j] is either 0 or 1
- All the empty cells can be visited from the starting position of the robot
- Robot starts at an empty cell (room[row][col] = 1)
- You cannot access the room matrix directly - must use Robot API only
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code