|
| 1 | +[](https://github.com/javadev/LeetCode-in-Java) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Java/fork) |
| 3 | + |
| 4 | +## 3568\. Minimum Moves to Clean the Classroom |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +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: |
| 9 | + |
| 10 | +* `'S'`: Starting position of the student |
| 11 | +* `'L'`: Litter that must be collected (once collected, the cell becomes empty) |
| 12 | +* `'R'`: Reset area that restores the student's energy to full capacity, regardless of their current energy level (can be used multiple times) |
| 13 | +* `'X'`: Obstacle the student cannot pass through |
| 14 | +* `'.'`: Empty space |
| 15 | + |
| 16 | +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'`. |
| 17 | + |
| 18 | +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`. |
| 19 | + |
| 20 | +Return the **minimum** number of moves required to collect all litter items, or `-1` if it's impossible. |
| 21 | + |
| 22 | +**Example 1:** |
| 23 | + |
| 24 | +**Input:** classroom = ["S.", "XL"], energy = 2 |
| 25 | + |
| 26 | +**Output:** 2 |
| 27 | + |
| 28 | +**Explanation:** |
| 29 | + |
| 30 | +* The student starts at cell `(0, 0)` with 2 units of energy. |
| 31 | +* Since cell `(1, 0)` contains an obstacle 'X', the student cannot move directly downward. |
| 32 | +* A valid sequence of moves to collect all litter is as follows: |
| 33 | + * Move 1: From `(0, 0)` → `(0, 1)` with 1 unit of energy and 1 unit remaining. |
| 34 | + * Move 2: From `(0, 1)` → `(1, 1)` to collect the litter `'L'`. |
| 35 | +* The student collects all the litter using 2 moves. Thus, the output is 2. |
| 36 | + |
| 37 | +**Example 2:** |
| 38 | + |
| 39 | +**Input:** classroom = ["LS", "RL"], energy = 4 |
| 40 | + |
| 41 | +**Output:** 3 |
| 42 | + |
| 43 | +**Explanation:** |
| 44 | + |
| 45 | +* The student starts at cell `(0, 1)` with 4 units of energy. |
| 46 | +* A valid sequence of moves to collect all litter is as follows: |
| 47 | + * Move 1: From `(0, 1)` → `(0, 0)` to collect the first litter `'L'` with 1 unit of energy used and 3 units remaining. |
| 48 | + * Move 2: From `(0, 0)` → `(1, 0)` to `'R'` to reset and restore energy back to 4. |
| 49 | + * Move 3: From `(1, 0)` → `(1, 1)` to collect the second litter `'L'`. |
| 50 | +* The student collects all the litter using 3 moves. Thus, the output is 3. |
| 51 | + |
| 52 | +**Example 3:** |
| 53 | + |
| 54 | +**Input:** classroom = ["L.S", "RXL"], energy = 3 |
| 55 | + |
| 56 | +**Output:** \-1 |
| 57 | + |
| 58 | +**Explanation:** |
| 59 | + |
| 60 | +No valid path collects all `'L'`. |
| 61 | + |
| 62 | +**Constraints:** |
| 63 | + |
| 64 | +* `1 <= m == classroom.length <= 20` |
| 65 | +* `1 <= n == classroom[i].length <= 20` |
| 66 | +* `classroom[i][j]` is one of `'S'`, `'L'`, `'R'`, `'X'`, or `'.'` |
| 67 | +* `1 <= energy <= 50` |
| 68 | +* There is exactly **one** `'S'` in the grid. |
| 69 | +* There are **at most** 10 `'L'` cells in the grid. |
| 70 | + |
| 71 | +## Solution |
| 72 | + |
| 73 | +```java |
| 74 | +import java.util.ArrayDeque; |
| 75 | +import java.util.ArrayList; |
| 76 | +import java.util.Arrays; |
| 77 | +import java.util.List; |
| 78 | +import java.util.Queue; |
| 79 | + |
| 80 | +@SuppressWarnings({"java:S135", "java:S6541"}) |
| 81 | +public class Solution { |
| 82 | + private static class State { |
| 83 | + int x; |
| 84 | + int y; |
| 85 | + int energy; |
| 86 | + int mask; |
| 87 | + int steps; |
| 88 | + |
| 89 | + State(int x, int y, int energy, int mask, int steps) { |
| 90 | + this.x = x; |
| 91 | + this.y = y; |
| 92 | + this.energy = energy; |
| 93 | + this.mask = mask; |
| 94 | + this.steps = steps; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public int minMoves(String[] classroom, int energy) { |
| 99 | + int m = classroom.length; |
| 100 | + int n = classroom[0].length(); |
| 101 | + char[][] grid = new char[m][n]; |
| 102 | + for (int i = 0; i < m; i++) { |
| 103 | + grid[i] = classroom[i].toCharArray(); |
| 104 | + } |
| 105 | + int startX = -1; |
| 106 | + int startY = -1; |
| 107 | + List<int[]> lumetarkon = new ArrayList<>(); |
| 108 | + for (int i = 0; i < m; i++) { |
| 109 | + for (int j = 0; j < n; j++) { |
| 110 | + char c = grid[i][j]; |
| 111 | + if (c == 'S') { |
| 112 | + startX = i; |
| 113 | + startY = j; |
| 114 | + } else if (c == 'L') { |
| 115 | + lumetarkon.add(new int[] {i, j}); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + int totalLitter = lumetarkon.size(); |
| 120 | + int allMask = (1 << totalLitter) - 1; |
| 121 | + int[][][] visited = new int[m][n][1 << totalLitter]; |
| 122 | + for (int[][] layer : visited) { |
| 123 | + for (int[] row : layer) { |
| 124 | + Arrays.fill(row, -1); |
| 125 | + } |
| 126 | + } |
| 127 | + Queue<State> queue = new ArrayDeque<>(); |
| 128 | + queue.offer(new State(startX, startY, energy, 0, 0)); |
| 129 | + visited[startX][startY][0] = energy; |
| 130 | + int[][] dirs = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}}; |
| 131 | + while (!queue.isEmpty()) { |
| 132 | + State curr = queue.poll(); |
| 133 | + if (curr.mask == allMask) { |
| 134 | + return curr.steps; |
| 135 | + } |
| 136 | + for (int[] dir : dirs) { |
| 137 | + int nx = curr.x + dir[0]; |
| 138 | + int ny = curr.y + dir[1]; |
| 139 | + if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') { |
| 140 | + continue; |
| 141 | + } |
| 142 | + int nextEnergy = curr.energy - 1; |
| 143 | + if (nextEnergy < 0) { |
| 144 | + continue; |
| 145 | + } |
| 146 | + char cell = grid[nx][ny]; |
| 147 | + if (cell == 'R') { |
| 148 | + nextEnergy = energy; |
| 149 | + } |
| 150 | + int nextMask = curr.mask; |
| 151 | + if (cell == 'L') { |
| 152 | + for (int i = 0; i < lumetarkon.size(); i++) { |
| 153 | + int[] pos = lumetarkon.get(i); |
| 154 | + if (pos[0] == nx && pos[1] == ny) { |
| 155 | + nextMask |= (1 << i); |
| 156 | + break; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + if (visited[nx][ny][nextMask] < nextEnergy) { |
| 161 | + visited[nx][ny][nextMask] = nextEnergy; |
| 162 | + queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1)); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + return -1; |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
0 commit comments