Easy & Clear Solution (Java, c++, Python3)
\n\n### Problem Explanation\n\nIn this problem, you are navigating through a dungeon represented by a grid of rooms. Each room has a specified minimum time (mov
\n\n### Problem Explanation\n\nIn this problem, you are navigating through a dungeon represented by a grid of rooms. Each room has a specified minimum time (`moveTime[i][j]`) indicating when you can start moving into that room. You begin at the top-left room `(0, 0)` at time `t = 0` and aim to reach the bottom-right room `(n - 1, m - 1)`. Moving between adjacent rooms takes alternating amounts of time\u20141 second for the first move and 2 seconds for the next move, and this alternation continues.\n\nTo solve this problem, we use a priority queue to explore rooms based on the minimum time to reach them. We keep track of the minimum time required to enter each room and update our path accordingly.\n\n### Python Implementation\n\nHere\u2019s the Python implementation of the solution with meaningful variable names:\n\n```python\nclass Solution:\n    def minTimeToReach(self, moveTime: List[List[int]]) -> int:\n        # Store the input moveTime in a variable named roomMoveTime\n        roomMoveTime = moveTime  \n        totalRows = len(roomMoveTime)\n        totalCols = len(roomMoveTime[0])\n        \n        # Priority queue to store (current_time, row, col, step_cost)\n        priorityQueue = [(0, 0, 0, 1)]  # Start at (0, 0) with time 0 and step cost 1\n        minimumArrivalTime = [[float(\'inf\')] * totalCols for _ in range(totalRows)]\n        minimumArrivalTime[0][0] = 0\n\n        # Directions for adjacent rooms (down, up, right, left)\n        adjacentDirections = [(1, 0), (-1, 0), (0, 1), (0, -1)]\n\n        while priorityQueue:\n            currentTime, currentRow, currentCol, currentStepCost = heapq.heappop(priorityQueue)\n\n            # If we reached the target room (totalRows - 1, totalCols - 1)\n            if (currentRow, currentCol) == (totalRows - 1, totalCols - 1):\n                return currentTime\n\n            # Explore adjacent rooms\n            for dx, dy in adjacentDirections:\n                nextRow, nextCol = currentRow + dx, currentCol + dy\n\n                if 0 <= nextRow < totalRows and 0 <= nextCol < totalCols:\n                    waitTime = max(roomMoveTime[nextRow][nextCol] - currentTime, 0)\n                    newArrivalTime = currentTime + currentStepCost + waitTime\n\n                    # Only push to the queue if we found a better arrival time\n                    if newArrivalTime < minimumArrivalTime[nextRow][nextCol]:\n                        minimumArrivalTime[nextRow][nextCol] = newArrivalTime\n                        nextStepCost = 1 if currentStepCost == 2 else 2\n                        heapq.heappush(priorityQueue, (newArrivalTime, nextRow, nextCol, nextStepCost))\n\n        return -1  # Return -1 if the target room is unreachable\n```\n\n### C++ Implementation\n\nHere\u2019s the C++ implementation with clear variable names:\n\n```cpp\nclass Solution {\npublic:\n    int minTimeToReach(std::vector<std::vector<int>>& moveTime) {\n        // Store the input moveTime in a variable named roomMoveTime\n        std::vector<std::vector<int>> roomMoveTime = moveTime;  \n        int totalRows = roomMoveTime.size();\n        int totalCols = roomMoveTime[0].size();\n        \n        // Priority queue to store (current_time, row, col, step_cost)\n        using State = std::tuple<int, int, int, int>; // (current_time, row, col, step_cost)\n        std::priority_queue<State, std::vector<State>, std::greater<State>> priorityQueue;\n        priorityQueue.emplace(0, 0, 0, 1);  // Start at (0, 0) with time 0 and step cost 1\n        \n        std::vector<std::vector<int>> minimumArrivalTime(totalRows, std::vector<int>(totalCols, INT_MAX));\n        minimumArrivalTime[0][0] = 0;\n\n        // Directions for adjacent rooms (down, up, right, left)\n        std::vector<std::pair<int, int>> adjacentDirections = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n        while (!priorityQueue.empty()) {\n            auto [currentTime, currentRow, currentCol, currentStepCost] = priorityQueue.top();\n            priorityQueue.pop();\n\n            // If we reached the target room (totalRows - 1, totalCols - 1)\n            if (currentRow == totalRows - 1 && currentCol == totalCols - 1) {\n                return currentTime;\n            }\n\n            // Explore adjacent rooms\n            for (const auto& [dx, dy] : adjacentDirections) {\n                int nextRow = currentRow + dx;\n                int nextCol = currentCol + dy;\n\n                if (nextRow >= 0 && nextRow < totalRows && nextCol >= 0 && nextCol < totalCols) {\n                    int waitTime = std::max(roomMoveTime[nextRow][nextCol] - currentTime, 0);\n                    int newArrivalTime = currentTime + currentStepCost + waitTime;\n\n                    // Only push to the queue if we found a better arrival time\n                    if (newArrivalTime < minimumArrivalTime[nextRow][nextCol]) {\n                        minimumArrivalTime[nextRow][nextCol] = newArrivalTime;\n                        int nextStepCost = (currentStepCost == 2) ? 1 : 2;\n                        priorityQueue.emplace(newArrivalTime, nextRow, nextCol, nextStepCost);\n                    }\n                }\n            }\n        }\n\n        return -1; // Return -1 if the target room is unreachable\n    }\n};\n```\n\n### Java Implementation\n\nAnd here\u2019s the Java implementation with clear variable names:\n\n```java\n\nclass Solution {\n    public int minTimeToReach(int[][] moveTime) {\n        // Store the input moveTime in a variable named roomMoveTime\n        int[][] roomMoveTime = moveTime;  \n        int totalRows = roomMoveTime.length;\n        int totalCols = roomMoveTime[0].length;\n\n        // Priority queue to store (current_time, row, col, step_cost)\n        PriorityQueue<int[]> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));\n        priorityQueue.offer(new int[]{0, 0, 0, 1});  // Start at (0, 0) with time 0 and step cost 1\n        \n        int[][] minimumArrivalTime = new int[totalRows][totalCols];\n        for (int[] row : minimumArrivalTime) {\n            Arrays.fill(row, Integer.MAX_VALUE);\n        }\n        minimumArrivalTime[0][0] = 0;\n\n        // Directions for adjacent rooms (down, up, right, left)\n        int[][] adjacentDirections = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n\n        while (!priorityQueue.isEmpty()) {\n            int[] currentState = priorityQueue.poll();\n            int currentTime = currentState[0], currentRow = currentState[1], currentCol = currentState[2], currentStepCost = currentState[3];\n\n            // If we reached the target room (totalRows - 1, totalCols - 1)\n            if (currentRow == totalRows - 1 && currentCol == totalCols - 1) {\n                return currentTime;\n            }\n\n            // Explore adjacent rooms\n            for (int[] direction : adjacentDirections) {\n                int nextRow = currentRow + direction[0];\n                int nextCol = currentCol + direction[1];\n\n                if (nextRow >= 0 && nextRow < totalRows && nextCol >= 0 && nextCol < totalCols) {\n                    int waitTime = Math.max(roomMoveTime[nextRow][nextCol] - currentTime, 0);\n                    int newArrivalTime = currentTime + currentStepCost + waitTime;\n\n                    // Only push to the queue if we found a better arrival time\n                    if (newArrivalTime < minimumArrivalTime[nextRow][nextCol]) {\n                        minimumArrivalTime[nextRow][nextCol] = newArrivalTime;\n                        int nextStepCost = (currentStepCost == 2) ? 1 : 2;\n                        priorityQueue.offer(new int[]{newArrivalTime, nextRow, nextCol, nextStepCost});\n                    }\n                }\n            }\n        }\n\n        return -1; // Return -1 if the target room is unreachable\n    }\n}\n```\n
['C++', 'Java', 'Python3']