[C++] Dijkstra once again
Same approach Like solution: https://leetcode.com/problems/find-minimum-time-to-reach-last-room-i/solutions/6011112/c-dijkstra-algorithm-implemented-using-a-set
Same approach Like solution: https://leetcode.com/problems/find-minimum-time-to-reach-last-room-i/solutions/6011112/c-dijkstra-algorithm-implemented-using-a-set/\nA Dijkstra implementation processing the cells with the smallest time first.\nThe difference between the 2 solutions is that we keep track also of the distance in cells in order to add + 1 or +2 alternatively.\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minTimeToReach(vector<vector<int>>& moveTime) {\n        vector<int> di = {0, 0, 1, -1}, dj = {1, -1, 0, 0};\n        int n = moveTime.size(), m = moveTime[0].size();\n        vector<vector<int>> t(n + 1, vector<int>(m + 1, -1)); //min time to reach[i][j]\n        set<vector<int>> ord_pos; // [time, i, j, distance]\n        t[0][0] = 0;\n        ord_pos.insert({0, 0, 0, 0}); //[0] -time, i, j, distance in cells.\n        while(!ord_pos.empty()) {\n            vector<int> front = *ord_pos.begin();\n            ord_pos.erase(ord_pos.begin());\n\n            if (t[front[1]][front[2]] != -1 && front[0] > t[front[1]][front[2]]) //no improvement\n                continue;\n            int d = (front[3] + 1) % 2 == 1 ? 1 : 2;\n            for (int k = 0; k < 4; k++) {\n                int newposi = front[1] + di[k];\n                int newposj = front[2] + dj[k];\n                if (!is_valid(newposi, newposj, n, m))\n                    continue;\n                \n                if (t[newposi][newposj] == -1) {\n                    if (front[0] <= moveTime[newposi][newposj]) {\n                        t[newposi][newposj] = moveTime[newposi][newposj] + d;\n                    } else {\n                        t[newposi][newposj] = front[0] + d;\n                    }\n                    ord_pos.insert({t[newposi][newposj], newposi, newposj, front[3] + 1});\n                    continue;\n                }\n                if (front[0] + d < t[newposi][newposj] && front[0] >= moveTime[newposi][newposj]) {\n                    t[newposi][newposj] = front[0] + d;\n                    ord_pos.insert({t[newposi][newposj], newposi, newposj, front[3] + 1});\n                }\n                \n            }\n        }\n        return t[n-1][m-1];\n    }\n    bool is_valid(int i, int j, int n, int m) {\n        return i >= 0 && j >= 0 && i < n && j < m;\n    }\n};\n```
['C++']