|
| 1 | +package g3501_3600.s3568_minimum_moves_to_clean_the_classroom; |
| 2 | + |
| 3 | +// #Medium #Array #Hash_Table #Breadth_First_Search #Matrix #Bit_Manipulation |
| 4 | +// #2025_06_03_Time_94_ms_(99.86%)_Space_53.76_MB_(99.86%) |
| 5 | + |
| 6 | +import java.util.ArrayDeque; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Queue; |
| 11 | + |
| 12 | +@SuppressWarnings({"java:S135", "java:S6541"}) |
| 13 | +public class Solution { |
| 14 | + private static class State { |
| 15 | + int x; |
| 16 | + int y; |
| 17 | + int energy; |
| 18 | + int mask; |
| 19 | + int steps; |
| 20 | + |
| 21 | + State(int x, int y, int energy, int mask, int steps) { |
| 22 | + this.x = x; |
| 23 | + this.y = y; |
| 24 | + this.energy = energy; |
| 25 | + this.mask = mask; |
| 26 | + this.steps = steps; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + public int minMoves(String[] classroom, int energy) { |
| 31 | + int m = classroom.length; |
| 32 | + int n = classroom[0].length(); |
| 33 | + char[][] grid = new char[m][n]; |
| 34 | + for (int i = 0; i < m; i++) { |
| 35 | + grid[i] = classroom[i].toCharArray(); |
| 36 | + } |
| 37 | + int startX = -1; |
| 38 | + int startY = -1; |
| 39 | + List<int[]> lumetarkon = new ArrayList<>(); |
| 40 | + for (int i = 0; i < m; i++) { |
| 41 | + for (int j = 0; j < n; j++) { |
| 42 | + char c = grid[i][j]; |
| 43 | + if (c == 'S') { |
| 44 | + startX = i; |
| 45 | + startY = j; |
| 46 | + } else if (c == 'L') { |
| 47 | + lumetarkon.add(new int[] {i, j}); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + int totalLitter = lumetarkon.size(); |
| 52 | + int allMask = (1 << totalLitter) - 1; |
| 53 | + int[][][] visited = new int[m][n][1 << totalLitter]; |
| 54 | + for (int[][] layer : visited) { |
| 55 | + for (int[] row : layer) { |
| 56 | + Arrays.fill(row, -1); |
| 57 | + } |
| 58 | + } |
| 59 | + Queue<State> queue = new ArrayDeque<>(); |
| 60 | + queue.offer(new State(startX, startY, energy, 0, 0)); |
| 61 | + visited[startX][startY][0] = energy; |
| 62 | + int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; |
| 63 | + while (!queue.isEmpty()) { |
| 64 | + State curr = queue.poll(); |
| 65 | + if (curr.mask == allMask) { |
| 66 | + return curr.steps; |
| 67 | + } |
| 68 | + for (int[] dir : dirs) { |
| 69 | + int nx = curr.x + dir[0]; |
| 70 | + int ny = curr.y + dir[1]; |
| 71 | + if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') { |
| 72 | + continue; |
| 73 | + } |
| 74 | + int nextEnergy = curr.energy - 1; |
| 75 | + if (nextEnergy < 0) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + char cell = grid[nx][ny]; |
| 79 | + if (cell == 'R') { |
| 80 | + nextEnergy = energy; |
| 81 | + } |
| 82 | + int nextMask = curr.mask; |
| 83 | + if (cell == 'L') { |
| 84 | + for (int i = 0; i < lumetarkon.size(); i++) { |
| 85 | + int[] pos = lumetarkon.get(i); |
| 86 | + if (pos[0] == nx && pos[1] == ny) { |
| 87 | + nextMask |= (1 << i); |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + if (visited[nx][ny][nextMask] < nextEnergy) { |
| 93 | + visited[nx][ny][nextMask] = nextEnergy; |
| 94 | + queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1)); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return -1; |
| 99 | + } |
| 100 | +} |
0 commit comments