LeetCode-in-Java

3568. Minimum Moves to Clean the Classroom

Medium

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:

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:

Example 2:

Input: classroom = [“LS”, “RL”], energy = 4

Output: 3

Explanation:

Example 3:

Input: classroom = [“L.S”, “RXL”], energy = 3

Output: -1

Explanation:

No valid path collects all 'L'.

Constraints:

Solution

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;

@SuppressWarnings({"java:S135", "java:S6541"})
public class Solution {
    private static class State {
        int x;
        int y;
        int energy;
        int mask;
        int steps;

        State(int x, int y, int energy, int mask, int steps) {
            this.x = x;
            this.y = y;
            this.energy = energy;
            this.mask = mask;
            this.steps = steps;
        }
    }

    public int minMoves(String[] classroom, int energy) {
        int m = classroom.length;
        int n = classroom[0].length();
        char[][] grid = new char[m][n];
        for (int i = 0; i < m; i++) {
            grid[i] = classroom[i].toCharArray();
        }
        int startX = -1;
        int startY = -1;
        List<int[]> lumetarkon = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                char c = grid[i][j];
                if (c == 'S') {
                    startX = i;
                    startY = j;
                } else if (c == 'L') {
                    lumetarkon.add(new int[] {i, j});
                }
            }
        }
        int totalLitter = lumetarkon.size();
        int allMask = (1 << totalLitter) - 1;
        int[][][] visited = new int[m][n][1 << totalLitter];
        for (int[][] layer : visited) {
            for (int[] row : layer) {
                Arrays.fill(row, -1);
            }
        }
        Queue<State> queue = new ArrayDeque<>();
        queue.offer(new State(startX, startY, energy, 0, 0));
        visited[startX][startY][0] = energy;
        int[][] dirs = { {0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        while (!queue.isEmpty()) {
            State curr = queue.poll();
            if (curr.mask == allMask) {
                return curr.steps;
            }
            for (int[] dir : dirs) {
                int nx = curr.x + dir[0];
                int ny = curr.y + dir[1];
                if (nx < 0 || ny < 0 || nx >= m || ny >= n || grid[nx][ny] == 'X') {
                    continue;
                }
                int nextEnergy = curr.energy - 1;
                if (nextEnergy < 0) {
                    continue;
                }
                char cell = grid[nx][ny];
                if (cell == 'R') {
                    nextEnergy = energy;
                }
                int nextMask = curr.mask;
                if (cell == 'L') {
                    for (int i = 0; i < lumetarkon.size(); i++) {
                        int[] pos = lumetarkon.get(i);
                        if (pos[0] == nx && pos[1] == ny) {
                            nextMask |= (1 << i);
                            break;
                        }
                    }
                }
                if (visited[nx][ny][nextMask] < nextEnergy) {
                    visited[nx][ny][nextMask] = nextEnergy;
                    queue.offer(new State(nx, ny, nextEnergy, nextMask, curr.steps + 1));
                }
            }
        }
        return -1;
    }
}