Not so efficient yet coded by my own dijkstra based solution
IntuitionThe problem requires finding the minimum time to travel from the top-left corner to the bottom-right corner of a grid while following specific movement
# Intuition
The problem requires finding the minimum time to travel from the top-left corner to the bottom-right corner of a grid while following specific movement constraints. Since the grid contains values that determine the waiting time at each cell, we need to decide the best route efficiently. This is a shortest-path problem in a weighted grid, and we use Dijkstra’s algorithm with a priority queue (min-heap) to always expand the least time-consuming path first.

# Approach
Edge Case Check:
If both grid[0][1] > 1 and grid[1][0] > 1, it is impossible to move from the starting position, so return -1.
Initialization

Create a minTime matrix initialized to INT_MAX to store the minimum time to reach each cell.
Create a visited matrix to track visited cells.
Define movement directions (up, down, left, right).
Use a min-heap (priority queue) that stores {current_time, row, col} to explore cells in increasing order of time.
Dijkstra’s Algorithm using Priority Queue

Start with {0, 0, 0} (time at the starting cell is 0).
While the queue is not empty:
Extract the cell with the minimum time.
If we reach (m-1, n-1), return the time.
If the cell is already visited, continue.
Mark the cell as visited.
Explore all four possible directions.
For each neighbor (Nrow, Ncol):
If out of bounds or already visited, skip.
If the next cell’s value ≤ currTime + 1, move immediately (currTime + 1).
If grid[Nrow][Ncol] > currTime + 1:
If the difference is even, wait 1 extra unit (grid[Nrow][Ncol] + 1).
If the difference is odd, move at exactly grid[Nrow][Ncol].
Return -1

If we never reach the destination, return -1.


# Complexity
- Time complexity:𝑂(𝑚×𝑛log⁡(𝑚×𝑛))O(m×nlog(m×n))

- Space complexity: O(mxn)

# Code
```cpp []
class Solution {
public:
    int minimumTime(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        if (grid[0][1] > 1 && grid[1][0] > 1)
            return -1;
        vector<vector<int>> minTime(m, vector<int>(n, INT_MAX));
        vector<vector<bool>> visited(m, vector<bool>(n, false));
        vector<vector<int>> directions = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;
        pq.push({grid[0][0], 0, 0});
        minTime[0][0] = 0;
        while (!pq.empty()) {
            auto curr = pq.top();
            pq.pop();
            int currTime = curr[0], row = curr[1], col = curr[2];
            if (row == m - 1 && col == n - 1) return currTime;
            if(visited[row][col]) continue;
            visited[row][col] = true;
            for (auto& dir : directions) {
                int Nrow = row + dir[0];
                int Ncol = col + dir[1];
                if(Nrow<0 || Nrow>=m || Ncol<0 || Ncol>=n || visited[Nrow][Ncol]) continue;
                if(grid[Nrow][Ncol] <= currTime+1){
                    pq.push({currTime+1, Nrow, Ncol});
                    minTime[Nrow][Ncol] = currTime+1;
                } else if((grid[Nrow][Ncol]-currTime)%2 == 0){
                    pq.push({grid[Nrow][Ncol]+1, Nrow, Ncol});
                    minTime[Nrow][Ncol] = grid[Nrow][Ncol] + 1;
                }else{
                    pq.push({grid[Nrow][Ncol], Nrow, Ncol});
                    minTime[Nrow][Ncol] = grid[Nrow][Ncol];
                }
            }
        }
        return -1;
    }
};
```
['Shortest Path', 'C++']