Minimum Moves to Clean the Classroom Problem
Description
LeetCode Problem 3568.
You are given an m x n grid classroom where a student volunteer is tasked with cleaning up litter scattered around the room. Each cell in the grid is one of the following:
'S': Starting position of the student'L': Litter that must be collected (once collected, the cell becomes empty)'R': Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times)'X': Obstacle the student cannot pass through'.': Empty space
You are also given an integer energy, representing the student's maximum energy capacity. The student starts with this energy from the starting position 'S'.
Each move to an adjacent cell (up, down, left, or right) costs 1 unit of energy. If the energy reaches 0, the student can only continue if they are on a reset area 'R', which resets the energy to its maximum capacity energy.
Return the minimum number of moves required to collect all litter items, or -1 if it's impossible.
Example 1:
Input: classroom = ["S.", "XL"], energy = 2
Output: 2
Explanation:
- The student starts at cell (0, 0) with 2 units of energy.
- Since cell (1, 0) contains an obstacle 'X', the student cannot move directly downward.
- A valid sequence of moves to collect all litter is as follows:
- Move 1: From (0, 0) → (0, 1) with 1 unit of energy and 1 unit remaining.
- Move 2: From (0, 1) → (1, 1) to collect the litter 'L'.
- The student collects all the litter using 2 moves. Thus, the output is 2.
Example 2:
Input: classroom = ["LS", "RL"], energy = 4
Output: 3
Explanation:
- The student starts at cell (0, 1) with 4 units of energy.
- A valid sequence of moves to collect all litter is as follows:
- Move 1: From (0, 1) → (0, 0) to collect the first litter 'L' with 1 unit of energy used and 3 units remaining.
- Move 2: From (0, 0) → (1, 0) to 'R' to reset and restore energy back to 4.
- Move 3: From (1, 0) → (1, 1) to collect the second litter 'L'.
- The student collects all the litter using 3 moves. Thus, the output is 3.
Example 3:
Input: classroom = ["L.S", "RXL"], energy = 3
Output: -1
Explanation:
No valid path collects all 'L'.
Constraints:
1 <= m == classroom.length <= 201 <= n == classroom[i].length <= 20classroom[i][j]is one of'S','L','R','X', or'.'1 <= energy <= 50- There is exactly one
'S'in the grid. - There are at most 10
'L'cells in the grid.
Solution
Approach: BFS over (position, energy, litter)
Find the start S and number the at most 10 litter cells L. A state is the current cell, the remaining energy, and a bitmask of the litter still on the floor. Breadth-first search from the start with full energy and all litter present, so the first time the mask becomes empty gives the fewest moves.
Every move to a neighboring non-X cell costs 1 energy and is only allowed with energy left. Stepping on an L clears its bit, and stepping on an R restores the energy to its maximum. Keep a visited set of states to avoid repeats. If the search ends without clearing all litter, return -1.
Complexity: time and space O(m * n * energy * 2^c), where c is the number of litter cells.
class Solution:
def minMoves(self, classroom: List[str], energy: int) -> int:
m, n = len(classroom), len(classroom[0])
d = [[0] * n for _ in range(m)]
x = y = cnt = 0
for i, row in enumerate(classroom):
for j, c in enumerate(row):
if c == "S":
x, y = i, j
elif c == "L":
d[i][j] = cnt
cnt += 1
if cnt == 0:
return 0
vis = [
[[[False] * (1 << cnt) for _ in range(energy + 1)] for _ in range(n)]
for _ in range(m)
]
q = [(x, y, energy, (1 << cnt) - 1)]
vis[x][y][energy][(1 << cnt) - 1] = True
dirs = (-1, 0, 1, 0, -1)
ans = 0
while q:
t = q
q = []
for i, j, cur_energy, mask in t:
if mask == 0:
return ans
if cur_energy <= 0:
continue
for k in range(4):
x, y = i + dirs[k], j + dirs[k + 1]
if 0 <= x < m and 0 <= y < n and classroom[x][y] != "X":
nxt_energy = (
energy if classroom[x][y] == "R" else cur_energy - 1
)
nxt_mask = mask
if classroom[x][y] == "L":
nxt_mask &= ~(1 << d[x][y])
if not vis[x][y][nxt_energy][nxt_mask]:
vis[x][y][nxt_energy][nxt_mask] = True
q.append((x, y, nxt_energy, nxt_mask))
ans += 1
return -1