Video Explanation (With Intuition)
Explanation\n\nhttps://youtu.be/GAj_Q78lYP4\n\nClick here if the preview above doesn\'t works\n\nNote - Video will be public at 12:15PM IST.\n\n# Code\n\n#defin
# Explanation\n\nhttps://youtu.be/GAj_Q78lYP4\n\n[Click here if the preview above doesn\'t works](https://youtu.be/GAj_Q78lYP4)\n\nNote - Video will be public at 12:15PM IST.\n\n# Code\n```\n#define F first\n#define S second\n\nconst vector<vector<int>> neighbours = {\n    {0, 1},\n    {1, 0},\n    {0, -1},\n    {-1, 0}\n};\n\nstruct Node {\n    int time;\n    int row, col;\n    \n    Node (int _time, int _row, int _col) : time(_time), row(_row), col(_col) {}\n    \n    bool operator < (const Node& rhs) const {\n        if (time != rhs.time) return time < rhs.time;\n        if (row != rhs.row) return row < rhs.row;\n        return col < rhs.col;\n    }\n};\n\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n        int rows = grid.size();\n        int cols = grid[0].size(); \n        \n        if (grid[0][1] > 1 && grid[1][0] > 1) return -1;\n        \n        vector<vector<int>> time_to_reach(rows, vector<int>(cols, 1e9));\n        time_to_reach[0][0] = 0;\n        \n        set<Node> q;\n        for (int r = 0; r < rows; r ++) {\n            for (int c = 0; c < cols; c ++) {\n                q.insert(Node(time_to_reach[r][c], r, c));\n            }\n        }\n        \n        while (!q.empty()) {\n            Node cur = *q.begin();\n            q.erase (q.begin());\n            \n            for (auto i : neighbours) {\n                int r = cur.row + i[0], c = cur.col + i[1];\n                if (r < 0 || r >= rows || c < 0 || c >= cols) continue;\n                \n                int wait_time = max(0, grid[r][c] - cur.time - 1);\n                if (wait_time % 2 != 0) wait_time ++;\n            \n                int new_time  = cur.time + wait_time + 1;\n                if (time_to_reach[r][c] > new_time) {\n                    q.erase (Node(time_to_reach[r][c], r, c));\n                    time_to_reach[r][c] = new_time;\n                    q.insert (Node(time_to_reach[r][c], r, c));\n                }\n            }\n        }\n        \n        return time_to_reach[rows-1][cols-1];\n    }\n};\n```
['C++']