C++ | Dijkstra + flip-flopping | Simple Logic
```\nclass Solution {\npublic:\n    // To check whether the node is in range or not...\n    bool isValid(int row, int col, vector>& grid){\n        return row >
```\nclass Solution {\npublic:\n    // To check whether the node is in range or not...\n    bool isValid(int row, int col, vector<vector<int>>& grid){\n        return row >= 0 && col >= 0 && row < grid.size() && col < grid[0].size();\n    }\n    \n    int minimumTime(vector<vector<int>>& grid) {\n        \n        // min heap...\n        priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, greater<pair<int,pair<int,int>>>> pq;\n        \n        \n        int n = grid.size();\n        int m = grid[0].size();\n        \n        // Base cases...\n        if(n > 1 && m > 1){\n            if(grid[0][1] > grid[0][0] + 1 && grid[1][0] > grid[0][0] + 1){\n                return -1;\n            }\n        }\n        \n        if(n > 1 && m == 0){\n            if(grid[1][0] > grid[0][0] + 1){\n                return -1;\n            }\n        }\n        \n        if(n == 0 && m > 1){\n            if(grid[0][1] > grid[0][0] + 1){\n                return -1;\n            }\n        }\n        \n        // Initializing distance array...\n        vector<vector<int>> dists(n,vector<int>(m,INT_MAX));\n        \n        \n        int dr[] = {0,1,0,-1};\n        int dc[] = {-1,0,1,0};\n        \n        pq.push({0,{0,0}});\n        \n        dists[0][0] = 0;\n        \n        // Dijkstra\n        \n        while(!pq.empty()){\n            auto tops = pq.top();\n            pq.pop();\n            \n            int dist = tops.first;\n            \n            int row = tops.second.first;\n            int col = tops.second.second;\n            \n            if(row == grid.size() - 1 && col == grid[0].size() - 1){\n                return dist;\n                \n            }\n            \n            for(int i = 0 ; i < 4; i++){\n                \n                int newrow = row + dr[i];\n                int newcol = col + dc[i];\n                \n                // Simple as explained in the question conditions are applied...\n                \n                if(isValid(newrow,newcol,grid) && grid[newrow][newcol] <= dist + 1){\n                    \n                    if(dists[newrow][newcol] > dist + 1){\n                        pq.push({dist + 1, {newrow,newcol}});\n                        dists[newrow][newcol] = dist + 1;\n                        \n                    }\n                    \n                }\n                \n                // If the conditions are not matched then there must be a cell smaller than grid[row][col] that is the cell from which iterator reached on  grid[row][col]. It can go back and forth in those two cells increasing the time and making the iterator able to reach new cell... \n                \n                // Number of times the back and forth process occured will be grid[newrow][newcol] - (dist) - 1...\n                \n                // If this number is odd then to make the iterator reach again at grid[row][col] we add 1 in it...\n                \n                else if(isValid(newrow,newcol,grid) && grid[newrow][newcol] > dist + 1){\n                    \n                    int diff = grid[newrow][newcol] - (dist);\n                    diff--;\n                    \n                    if((diff&1)){\n                        diff++;\n                    }\n                    \n                    if(dists[newrow][newcol] > dist + diff + 1){\n                        pq.push({dist + diff + 1, {newrow,newcol}});\n                        dists[newrow][newcol] = dist + diff + 1;\n                    }\n                }\n                \n            }\n            \n        }\n        \n        return -1;\n        \n    }\n};
['Math', 'Breadth-First Search', 'Graph']