Dijkstra's Algorithm/easy.
IntuitionApproachTo understand the problem better we can consider moveTime as unlockTime.We move to the cell that unlocks the earliest only then we can reach th
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
To understand the problem better we can consider moveTime as unlockTime.We move to the cell that unlocks the earliest only then we can reach the cell `n-1,m-1` in the least possible time. Thus we need access to the cell that unlocks the earliest and we can use a `priority_queue` for the same, and with a slight modification to fill the `time/dist` array we can use the standard `Dijkstra's algorithm`.
We use the formula `2-(nrow+ncol)%2` to get the travelTime based on step (first/second). The formula is derived or based upon the indexes of the cell, for an odd cell use use travelTime=1 and for even cell we use travelTime=2.

# Complexity
- Time complexity:
O((n×m)×log(n×m))

- Space complexity:
O(n×m)

# Code
```cpp []
class Solution {
public:
    int minTimeToReach(vector<vector<int>>& grid) {
        priority_queue<pair<int, pair<int, int>>,
                       vector<pair<int, pair<int, int>>>, greater<>>
            pq;
        int n = grid.size(), m = grid[0].size();
        //{timeToReachCurrentCell,{i,j}};
        pq.push({0, {0, 0}});
        vector<vector<int>> time(n, vector<int>(m, -1));
        int drow[] = {-1, 0, 1, 0};
        int dcol[] = {0, 1, 0, -1};
        while (!pq.empty()) {
            auto it = pq.top();
            pq.pop();
            int currtime = it.first;
            int crow = it.second.first;
            int ccol = it.second.second;
            if (crow == n - 1 && ccol == m - 1)
                return currtime;
            for (int i = 0; i < 4; i++) {
                int nrow = crow + drow[i];
                int ncol = ccol + dcol[i];
                if (nrow >= 0 && nrow < n && ncol >= 0 && ncol < m &&
                    time[nrow][ncol] == -1) {
                    int travelTime = 2 - (nrow + ncol) % 2;
                    if (currtime >= grid[nrow][ncol]) {
                        time[nrow][ncol] = currtime + travelTime;
                        pq.push({time[nrow][ncol], {nrow, ncol}});
                    } else {
                        time[nrow][ncol] = grid[nrow][ncol] + travelTime;
                        pq.push({time[nrow][ncol], {nrow, ncol}});
                    }
                }
            }
        }
        return -1;
    }
};
```
['C++']