Number of Spaces Cleaning Robot Cleaned Problem
Description
LeetCode Problem 2061.
A room is represented by a 0-indexed 2D binary matrix room where a 0 represents an empty space and a 1 represents a space with an object. The top left corner of the room will be empty in all test cases.
A cleaning robot starts at the top left corner of the room and is facing right. The robot will continue heading straight until it reaches the edge of the room or it hits an object, after which it will turn 90 degrees clockwise and repeat this process. The starting space and all spaces that the robot visits are cleaned by it.
Return the number of clean spaces in the room if the robot runs indefinitely.
Example 1:

Input: room = [[0,0,0],[1,1,0],[0,0,0]]
Output: 7
Explanation:
1. The robot cleans the spaces at (0, 0), (0, 1), and (0, 2).
2. The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces down.
3. The robot cleans the spaces at (1, 2), and (2, 2).
4. The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces left.
5. The robot cleans the spaces at (2, 1), and (2, 0).
6. The robot has cleaned all 7 empty spaces, so return 7.
Example 2:

Input: room = [[0,1,0],[1,0,0],[0,0,0]]
Output: 1
Explanation:
1. The robot cleans the space at (0, 0).
2. The robot hits an object, so it turns 90 degrees clockwise and now faces down.
3. The robot hits an object, so it turns 90 degrees clockwise and now faces left.
4. The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces up.
5. The robot is at the edge of the room, so it turns 90 degrees clockwise and now faces right.
6. The robot is back at its starting position.
7. The robot has cleaned 1 space, so return 1.
Example 3:
Input: room = [[0,0,0],[0,0,0],[0,0,0]]
Output: 8
Constraints:
m == room.lengthn == room[r].length1 <= m, n <= 300room[r][c]is either0or1.room[0][0] == 0
Solution
Approach: Simulation
Simulate the robot exactly as described. A state is the cell plus the direction it faces (m * n * 4 states). The robot's next step depends only on its state, so once a state repeats, the robot is looping and nothing new will be cleaned.
At each step, mark the current cell as cleaned (counting it the first time), then look at the cell straight ahead: if it is inside the room and has no object, move there; otherwise turn 90 degrees clockwise in place. Stop as soon as the current state was already visited and return the number of cleaned cells. The loop is iterative, so large rooms cause no deep recursion.
Complexity: time O(m * n), space O(m * n).
class Solution:
def numberOfCleanRooms(self, room: List[List[int]]) -> int:
m, n = len(room), len(room[0])
dirs = (0, 1, 0, -1, 0)
vis = set()
clean = [[False] * n for _ in range(m)]
i = j = k = 0
ans = 0
while (i, j, k) not in vis:
vis.add((i, j, k))
if not clean[i][j]:
clean[i][j] = True
ans += 1
x, y = i + dirs[k], j + dirs[k + 1]
if 0 <= x < m and 0 <= y < n and room[x][y] == 0:
i, j = x, y
else:
k = (k + 1) % 4
return ans