Still a variation of Djikstra. The hardness is the identify the wandering?
Intuition\n Describe your first thoughts on how to solve this problem. \nOkay, where did I stuck? I understand the trick of wandering around, but i cannot think
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nOkay, where did I stuck? I understand the trick of wandering around, but i cannot think of using it as the optimal route to reach grid[i][j]. I mean the problem becomes simple when you have the min-Time to reach each grid[i][j] ?  The hardness comes from think about using this wandering odd/even to achieve this min-time \n\n(IDK, I keep questioning how could they know which one to continue, but this is djisktra, and I should focus on how to achieve djisktra, which is to find min-time to get to each grid[i][j])\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n    def minimumTime(self, grid: List[List[int]]) -> int:\n\n\n\n        #Why disktra? Keep a current time + keep the min possible next move?\n\n        # THe only case that I cant move is when surrounded at first step?\n\n        if grid[0][1] > 1 and grid[1][0] > 1:\n            return -1\n        \n\n        #It\'s still Djisktra, but the twist lied in identify the minimum time to reach that grid[i][j] if we need to wandering around\n        dirs = [(1,0), (0,1), (-1,0), (0,-1)]\n        ROW, COL = len(grid), len(grid[0])\n\n        visited = [[0 for i in range(COL)] for j in range(ROW)]\n        visited[0][0] = 1\n        \n        ans = -1\n        heap = [(0,0,0)]\n\n        while heap:\n            time, curR, curC = heapq.heappop(heap)\n            if (curR, curC) == (ROW-1, COL - 1):\n                ans = time\n                break\n            for dr, dc in dirs:\n                newR, newC = curR + dr, curC + dc\n                if newR >= 0 and newR < ROW and newC >= 0 and newC < COL and visited[newR][newC] == 0:\n                    \n                    needTime = grid[newR][newC]\n                    if needTime < time:\n                        needTime = time+1\n                    elif (needTime - time) % 2 == 0:\n                        needTime += 1\n                    heapq.heappush(heap, (needTime, newR, newC))\n                    visited[newR][newC] = 1\n            \n        return ans\n                    \n\n\n        \n\n\n\n```
['Python3']