C++ BFS/PQ Code with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach : bfs with priority queue\n Describe your approach to solving the problem.
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach : bfs with priority queue\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: (m*n)log(m*n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n        int row=grid.size(), col=grid[0].size(), ans=INT_MAX;\n        vector<vector<int>> direction{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};\n        vector<vector<bool>> visited(row, vector<bool>(col, false));\n        \n        priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n    \n        visited[0][0]=true;\n        \n        pq.push({0, 0, 0});\n        // do initial explore\n        if(grid[1][0]<=1){\n            pq.push({1, 1, 0});\n            visited[1][0]=true;\n        }\n        if(grid[0][1]<=1){\n            pq.push({1, 0, 1});\n            visited[0][1]=true;\n        }\n        \n        // if no initial round return 0\n        if(pq.size()==1){\n            return -1;\n        }\n        \n        while(pq.size()){\n            auto curr = pq.top();\n            pq.pop();\n            int time=curr[0];\n            int r=curr[1], c=curr[2];\n            \n            if(r==row-1 && c==col-1){\n                ans=min(ans, time);\n            }\n            \n            for(auto& i:direction){\n                int newR=r+i[0];\n                int newC=c+i[1];\n                if(newR>=0 && newR<row && newC>=0 && newC<col && visited[newR][newC]==false){\n                    if(time+1>=grid[newR][newC]){\n                        pq.push({time+1, newR, newC});\n                    // if difference between two cell is more than 1\n                    }else{\n                        int diff=grid[newR][newC]-time;\n                        // steps to get two next cell\n                        if(diff%2==0){\n                            pq.push({grid[newR][newC]+1, newR, newC});\n                        }else{\n                            pq.push({grid[newR][newC], newR, newC});\n                        }\n                    }\n                    visited[newR][newC]=true;\n                }\n            }\n        }\n        \n        \n        return ans;\n    }\n};\n```
['C++']