Easy Dijkstra's solution using min heap
Intution\nUsing Dijkstras \n1. For finding the shortest path from starting point  to ending point , the first approach comes in mind is dijkstras shortest path 
# Intution\nUsing Dijkstras \n1. For finding the shortest path from starting point  to ending point , the first approach comes in mind is dijkstras shortest path algorithm\n2. There is slight modification in algorithm according the question \n3. In even move it take +2 seconds and on odd +1 seconds\n4. We have take min heap to stor {time, row, column,turn};\nOther all is similar to normal dijkstras\'s algoirthm\n\n# Complexity\n- Time complexity:\nO(m * n * log(m * n))\n\n- Space complexity:\nO(m * n)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n    int n=moveTime.size();\n    int m=moveTime[0].size();\n\n    priority_queue<pair<long long,pair<long long,pair<long long,long long>>>,vector<pair<long long,pair<long long,pair<long long,long long>>>>,greater<pair<long long,pair<long long,pair<long long,long long>>>>>pq;\n        \n    vector<vector<long long>>dist(n,vector<long long>(m,INT_MAX));\n\n    pq.push({0,{0,{0,1}}});\n    dist[0][0]=0;\n\n    while(!pq.empty()){\n        long long time=pq.top().first;\n        int row=pq.top().second.first;\n        int col=pq.top().second.second.first;\n        int turn=pq.top().second.second.second;\n        pq.pop();\n\n        if(row==n-1 && col==m-1){\n            return time;\n        }\n\n        int drow[4]={-1,0,1,0};\n        int dcol[4]={0,1,0,-1};\n\n        for(int i=0;i<4;i++){\n            int nrow=row+drow[i];\n            int ncol=col+dcol[i];\n          \n            if(nrow>=0 && ncol>=0 && nrow<n && ncol<m){\n                long long maxtime;\n                if(turn==1){\n                maxtime=max(time,1ll*moveTime[nrow][ncol])+1;\n                }\n                else{\n                maxtime=max(time,1ll*moveTime[nrow][ncol])+2;\n                }\n                 \n                if( dist[nrow][ncol]>maxtime){\n                dist[nrow][ncol]=maxtime;\n                pq.push({maxtime,{nrow,{ncol,!turn}}});\n\n                }\n            }\n            }\n        }\n        return -1;\n    }\n};\n```\n```java []\nimport java.util.PriorityQueue;\n\nclass Solution {\n    public int minTimeToReach(int[][] moveTime) {\n        int n = moveTime.length;\n        int m = moveTime[0].length;\n\n        PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Long.compare(a[0], b[0]));\n        long[][] dist = new long[n][m];\n        for (int i = 0; i < n; i++) {\n            for (int j = 0; j < m; j++) {\n                dist[i][j] = Long.MAX_VALUE;\n            }\n        }\n\n        pq.offer(new int[]{0, 0, 0, 1}); // {time, row, col, turn}\n        dist[0][0] = 0;\n\n        int[] drow = {-1, 0, 1, 0};\n        int[] dcol = {0, 1, 0, -1};\n\n        while (!pq.isEmpty()) {\n            int[] curr = pq.poll();\n            long time = curr[0];\n            int row = curr[1];\n            int col = curr[2];\n            int turn = curr[3];\n\n            if (row == n - 1 && col == m - 1) {\n                return (int) time;\n            }\n\n            for (int i = 0; i < 4; i++) {\n                int nrow = row + drow[i];\n                int ncol = col + dcol[i];\n\n                if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m) {\n                    long maxTime = (turn == 1) ? Math.max(time, moveTime[nrow][ncol]) + 1 : Math.max(time, moveTime[nrow][ncol]) + 2;\n\n                    if (dist[nrow][ncol] > maxTime) {\n                        dist[nrow][ncol] = maxTime;\n                        pq.offer(new int[]{(int) maxTime, nrow, ncol, 1 - turn});\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n}\n\n```\n```python []\nimport heapq\nimport sys\n\nclass Solution:\n    def minTimeToReach(self, moveTime):\n        n, m = len(moveTime), len(moveTime[0])\n        pq = []\n        heapq.heappush(pq, (0, 0, 0, 1))  # (time, row, col, turn)\n        dist = [[sys.maxsize] * m for _ in range(n)]\n        dist[0][0] = 0\n\n        drow = [-1, 0, 1, 0]\n        dcol = [0, 1, 0, -1]\n\n        while pq:\n            time, row, col, turn = heapq.heappop(pq)\n\n            if row == n - 1 and col == m - 1:\n                return time\n\n            for i in range(4):\n                nrow, ncol = row + drow[i], col + dcol[i]\n\n                if 0 <= nrow < n and 0 <= ncol < m:\n                    maxtime = max(time, moveTime[nrow][ncol]) + (1 if turn == 1 else 2)\n\n                    if dist[nrow][ncol] > maxtime:\n                        dist[nrow][ncol] = maxtime\n                        heapq.heappush(pq, (maxtime, nrow, ncol, 1 - turn))\n\n        return -1\n\n```\n```javascript []\nclass Solution {\n    minTimeToReach(moveTime) {\n        const n = moveTime.length;\n        const m = moveTime[0].length;\n        \n        const pq = new MinPriorityQueue({ priority: x => x[0] });\n        const dist = Array.from({ length: n }, () => Array(m).fill(Number.MAX_SAFE_INTEGER));\n        \n        pq.enqueue([0, 0, 0, 1]); // [time, row, col, turn]\n        dist[0][0] = 0;\n\n        const drow = [-1, 0, 1, 0];\n        const dcol = [0, 1, 0, -1];\n\n        while (!pq.isEmpty()) {\n            const [time, row, col, turn] = pq.dequeue().element;\n\n            if (row === n - 1 && col === m - 1) {\n                return time;\n            }\n\n            for (let i = 0; i < 4; i++) {\n                const nrow = row + drow[i];\n                const ncol = col + dcol[i];\n\n                if (nrow >= 0 && ncol >= 0 && nrow < n && ncol < m) {\n                    const maxtime = Math.max(time, moveTime[nrow][ncol]) + (turn === 1 ? 1 : 2);\n\n                    if (dist[nrow][ncol] > maxtime) {\n                        dist[nrow][ncol] = maxtime;\n                        pq.enqueue([maxtime, nrow, ncol, 1 - turn]);\n                    }\n                }\n            }\n        }\n        return -1;\n    }\n}\n\n```\n![c2d27b08-ffbe-4bc6-8b29-5fc4d8effd5f_1723775276.9029489.jpeg](https://assets.leetcode.com/users/images/ee045271-b358-4ab2-8bd2-0ef65452398d_1730620624.687265.jpeg)\n
['Depth-First Search', 'Heap (Priority Queue)', 'Shortest Path', 'C++', 'Java', 'Python3', 'JavaScript']