Easy C++ Code || Runtime beats - 99.89% , Memory beats - 88.68%
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/0ff4f1e5-1714-438b-9d78-a41242e0f7ba_1732842432.1175325.png)\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```cpp []\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n\n        int m = grid.size();\n        int n = grid[0].size();\n\n        vector<int> visited(m * n, -1);\n\n        priority_queue<pair<int, int>, vector<pair<int, int>>,\n                       greater<pair<int, int>>>\n            q;\n\n        q.push({0, 0});\n        visited[0] = 0;\n        vector<int> dir = {0, -1, 0, 1, 0};\n\n        if (grid[1][0] > 1 && grid[0][1] > 1)\n            return -1;\n\n        while (q.size() > 0) {\n\n            auto node = q.top();\n            q.pop();\n\n            int row = node.second / n;\n            int col = node.second % n;\n\n            int val = node.second;\n\n            int t = node.first;\n\n            if (row == m - 1 && col == n - 1) {\n                return t;\n            }\n            for (int j = 0; j < 4; j++) {\n\n                int new_row = row + dir[j];\n                int new_col = col + dir[j + 1];\n\n                if (new_row < 0 || new_row >= m || new_col < 0 || new_col >= n)\n                    continue;\n\n                int val = new_row * n + new_col;\n                if (visited[val] != -1)\n                    continue;\n\n                if (grid[new_row][new_col] <= t + 1)\n                    visited[val] = t + 1;\n                else if ((t + 1) % 2 != grid[new_row][new_col] % 2)\n                    visited[val] = grid[new_row][new_col] + 1;\n                else\n                    visited[val] = grid[new_row][new_col];\n                q.push({visited[val], val});\n            }\n        }\n\n        return -1;\n    }\n};\n```
['Array', 'Breadth-First Search', 'Graph', 'Heap (Priority Queue)', 'Matrix', 'Shortest Path', 'Python', 'C++', 'Java', 'C#']