Python3 || 13 lines, heap and toggle  ||
Pretty much the same as the preceding problem, with the added feature that the step toggles between 1 and 2, which we achieve by replacing step with 3 -  step.\
Pretty much the same as the preceding problem, with the added feature that the step toggles between 1 and 2, which we achieve by replacing `step` with `3 -  step`.\n\n```python3 []\nclass Solution:\n    def minTimeToReach(self, moveTime: List[List[int]]) -> int:\n        \n        onGrid = lambda x, y: 0 <= x < n and 0 <= y < m\n        n, m, dx, dy = len(moveTime), len(moveTime[0]), 1, 0\n        heap = [(0, 0, 0, 1)]\n        seen = {(0,0)}\n\n        while heap:\n            time, x, y, step = heappop(heap)\n\n            if (x, y) == (n - 1, m - 1): return time\n\n            for _ in range(4):\n\n                X, Y, dx, dy = x + dx, y + dy, -dy, dx\n\n                if onGrid(X, Y) and (X, Y) not in seen:\n                    t = max(time, moveTime[X][Y]) + step\n                    heappush(heap, [t, X, Y, 3 - step])\n                    seen.add((X, Y))\n```\n```cpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        int n = moveTime.size(), m = moveTime[0].size();\n        auto onGrid = [&](int x, int y) { return 0 <= x && x < n && 0 <= y && y < m; };\n        \n        int dx = 1, dy = 0;\n        priority_queue<tuple<int, int, int, int>, vector<tuple<int, int, int, int>>, greater<>> heap;\n        heap.emplace(0, 0, 0, 1);\n\n        set<pair<int, int>> seen;\n        seen.emplace(0, 0);\n\n        while (!heap.empty()) {\n            auto [time, x, y, step] = heap.top();\n            heap.pop();\n\n            if (x == n - 1 && y == m - 1) return time;\n\n            for (int i = 0; i < 4; ++i) {\n                int X = x + dx, Y = y + dy;\n                int temp_dx = dx, temp_dy = dy;\n                dx = -temp_dy;\n                dy = temp_dx;\n\n                if (onGrid(X, Y) && !seen.count({X, Y})) {\n                    int t = max(time, moveTime[X][Y]) + step;\n                    heap.emplace(t, X, Y, 3 - step);\n                    seen.emplace(X, Y);}\n            }\n        }\n        return -1;\n    }\n};\n\n```\n```java []\nclass Solution {\n    public int minTimeToReach(int[][] moveTime) {\n        int n = moveTime.length, m = moveTime[0].length;\n        int dx = 1, dy = 0;\n\n        PriorityQueue<int[]> heap = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));\n        heap.add(new int[]{0, 0, 0, 1});\n\n        Set<String> seen = new HashSet<>();\n        seen.add("0,0");\n\n        while (!heap.isEmpty()) {\n            int[] current = heap.poll();\n            int time = current[0], x = current[1], y = current[2], step = current[3];\n\n            if (x == n - 1 && y == m - 1) return time;\n\n            for (int i = 0; i < 4; i++) {\n                int X = x + dx, Y = y + dy;\n                int tempDx = dx, tempDy = dy;\n                dx = -tempDy;\n                dy = tempDx;\n\n                String pos = X + "," + Y;\n                if (onGrid(X, Y, n, m) && !seen.contains(pos)) {\n                    int t = Math.max(time, moveTime[X][Y]) + step;\n                    heap.add(new int[]{t, X, Y, 3 - step});\n                    seen.add(pos);}\n            }\n        }\n        return -1; // Return -1 if unreachable\n    }\n    private boolean onGrid(int x, int y, int n, int m) {\n        return 0 <= x && x < n && 0 <= y && y < m;\n    }\n}\n\n```\n[https://leetcode.com/problems/find-minimum-time-to-reach-last-room-ii/submissions/1442140920/](https://leetcode.com/problems/find-minimum-time-to-reach-last-room-ii/submissions/1442140920/)\n\nI could be wrong, but I think that time complexity is *O*(*N* log *N*) and space complexity is *O*(*N*), in which *N* ~ `m * n` .
['C++', 'Java', 'Python3']