Number of Spaces Cleaning Robot Cleaned
Simulate a robot that turns right on obstacles, halting once it revisits a state already visited in the same direction.
O(m*n) Space O(m*n) Problem#
An curriculum-track problem. A cleaning robot starts at the top-left of an m x n grid facing right. Each step: if the cell ahead is free, move forward and clean it; if blocked (wall or obstacle), rotate 90 degrees clockwise. The robot stops if it revisits the same cell with the same orientation. Return the number of distinct cells cleaned.
Examples#
room = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]]→7room = [[0]]→1room = [[0,1],[0,0]]→3
Constraints#
1 <= m, n <= 100,room[i][j]is0(empty) or1(obstacle),room[0][0] == 0.
Approach#
Track the robot’s (row, col, dir) state. Cells visited are stored in a separate set so we count each unique cell only once. Halt when the current (r, c, dir) is in a visited-states set — that’s a cycle and no new cleaning can happen.
Solution#
class Solution {public: int numberOfCleanRooms(vector<vector<int>>& room) { int m = room.size(), n = room[0].size(); int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; set<tuple<int,int,int>> seenState; set<pair<int,int>> cleaned; int r = 0, c = 0, d = 0; while (!seenState.count({r, c, d})) { seenState.insert({r, c, d}); cleaned.insert({r, c}); int nr = r + dx[d], nc = c + dy[d]; if (nr < 0 || nr >= m || nc < 0 || nc >= n || room[nr][nc] == 1) { d = (d + 1) % 4; } else { r = nr; c = nc; } } return (int)cleaned.size(); }};func numberOfCleanRooms(room [][]int) int { m, n := len(room), len(room[0]) dx := []int{0, 1, 0, -1} dy := []int{1, 0, -1, 0} seenState := map[[3]int]bool{} cleaned := map[[2]int]bool{} r, c, d := 0, 0, 0 for !seenState[[3]int{r, c, d}] { seenState[[3]int{r, c, d}] = true cleaned[[2]int{r, c}] = true nr, nc := r+dx[d], c+dy[d] if nr < 0 || nr >= m || nc < 0 || nc >= n || room[nr][nc] == 1 { d = (d + 1) % 4 } else { r, c = nr, nc } } return len(cleaned)}class Solution: def numberOfCleanRooms(self, room: List[List[int]]) -> int: m, n = len(room), len(room[0]) dx = (0, 1, 0, -1) dy = (1, 0, -1, 0) seen_state = set() cleaned = set() r, c, d = 0, 0, 0 while (r, c, d) not in seen_state: seen_state.add((r, c, d)) cleaned.add((r, c)) nr, nc = r + dx[d], c + dy[d] if nr < 0 or nr >= m or nc < 0 or nc >= n or room[nr][nc] == 1: d = (d + 1) % 4 else: r, c = nr, nc return len(cleaned)function numberOfCleanRooms(room) { const m = room.length, n = room[0].length; const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; const seenState = new Set(); const cleaned = new Set(); let r = 0, c = 0, d = 0; while (!seenState.has(`${r},${c},${d}`)) { seenState.add(`${r},${c},${d}`); cleaned.add(`${r},${c}`); const nr = r + dx[d], nc = c + dy[d]; if (nr < 0 || nr >= m || nc < 0 || nc >= n || room[nr][nc] === 1) { d = (d + 1) % 4; } else { r = nr; c = nc; } } return cleaned.size;}class Solution { public int numberOfCleanRooms(int[][] room) { int m = room.length, n = room[0].length; int[] dx = {0, 1, 0, -1}; int[] dy = {1, 0, -1, 0}; Set<Integer> seenState = new HashSet<>(); Set<Integer> cleaned = new HashSet<>(); int r = 0, c = 0, d = 0; while (!seenState.contains(((r * n + c) << 2) | d)) { seenState.add(((r * n + c) << 2) | d); cleaned.add(r * n + c); int nr = r + dx[d], nc = c + dy[d]; if (nr < 0 || nr >= m || nc < 0 || nc >= n || room[nr][nc] == 1) { d = (d + 1) % 4; } else { r = nr; c = nc; } } return cleaned.size(); }}function numberOfCleanRooms(room: number[][]): number { const m = room.length, n = room[0].length; const dx = [0, 1, 0, -1]; const dy = [1, 0, -1, 0]; const seenState = new Set<string>(); const cleaned = new Set<string>(); let r = 0, c = 0, d = 0; while (!seenState.has(`${r},${c},${d}`)) { seenState.add(`${r},${c},${d}`); cleaned.add(`${r},${c}`); const nr = r + dx[d], nc = c + dy[d]; if (nr < 0 || nr >= m || nc < 0 || nc >= n || room[nr][nc] === 1) { d = (d + 1) % 4; } else { r = nr; c = nc; } } return cleaned.size;}Editorial#
The state space (row, col, direction) is bounded by 4 * m * n, so the simulation halts in O(m*n) steps. The cleaned set is separate because we don’t care about direction when counting unique cells.
Complexity#
- Time: O(m*n).
- Space: O(m*n).
Concept revision#
Related#