2812. Find the Safest Path in a Grid

class Cell implements Comparable<Cell> {
    int x, y, maxWeight;

    public Cell(int x, int y, int maxWeight) {
        this.x = x;
        this.y = y;
        this.maxWeight = maxWeight;
    }

    public int compareTo(Cell other) {
        return Integer.compare(other.maxWeight, this.maxWeight);
    }
}


class Solution {
    public int maximumSafenessFactor(List<List<Integer>> grid) {
        
        Integer ROW = grid.size(), COL = grid.get(0).size();

        int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

        Queue<int[]> queue = new LinkedList<int[]>();

        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                if (grid.get(i).get(j) == 1) {
                    queue.offer(new int[]{i, j});
                    grid.get(i).set(j, 0);
                } else {
                    grid.get(i).set(j, -1);
                }
            }
        }

        while (!queue.isEmpty()) {
            int[] cur = queue.poll();
            int row = cur[0], col = cur[1];

            for (int i = 0; i < 4; i++) {
                int newRow = row + dir[i][0], newCol = col + dir[i][1];

                if (newRow < 0 || newRow >= ROW || newCol < 0 || newCol >= COL || grid.get(newRow).get(newCol) != -1) {
                    continue;
                }

                queue.offer(new int[]{newRow, newCol});
                grid.get(newRow).set(newCol, grid.get(row).get(col) + 1);
            }
        }


        boolean[][] visited = new boolean[ROW][COL];
        PriorityQueue<Cell> pq = new PriorityQueue<>();


        pq.offer(new Cell(0, 0, grid.get(0).get(0)));
        visited[0][0] = true;
        while (!pq.isEmpty()) {
            Cell current = pq.poll();
            if (current.x == ROW - 1 && current.y == COL - 1) {
                return current.maxWeight;
            }

            for (int[] r : dir) {
                int newX = current.x + r[0];
                int newY = current.y + r[1];
                if (newX >= 0 && newX < ROW && newY >= 0 && newY < COL && !visited[newX][newY]) {
                    visited[newX][newY] = true;
                    int newMaxWeight = Math.min(current.maxWeight, grid.get(newX).get(newY));           
                    pq.offer(new Cell(newX, newY, newMaxWeight));
                }
            }
        }
        System.out.println(grid);

        return 0;
    }
}