[C++] Easy hard
Notice that you can move back and forth multiple times to reach a single cell, meaning you can use a previous cell to go back and return to the current cell as 
Notice that you can move back and forth multiple times to reach a single cell, meaning you can use a previous cell to go back and return to the current cell as many times as necessary to accumulate the required time. The only situation where moving back and forth is not possible is at the very first move, `-1` will be returned when `grid[0][1] > 1 && grid[1][0] > 1`.\n\nI keep a two-dimensional array t, where `t[i][j]` represents the minimum time to arrive cell `(i,j)`.\nTo move from cell `(i,j)` to an adjacent cell `(ii, jj)` we handle the following cases:\n  - `grid[ii][jj] <= top[0] + 1`, we can directly move to the cell without any delay.\n  - `grid[ii][jj] > top[0] + 1`, we need to move back and forth to a previous cell.Let `diff = grid[ii][jj] - t[i][j]`, then:\n    - When diff is even the minimum time will `grid[ii][jj] + 1`\n    - othewise `grid[ii][jj]`.\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minimumTime(vector<vector<int>>& grid) {\n        if (grid[0][1] > 1 && grid[1][0] > 1)\n            return -1;\n        vector<int> di = {0, 0, -1, 1};\n        vector<int> dj = {-1, 1, 0, 0};\n        int n = grid.size(), m = grid[0].size();\n        vector<vector<int>>t(n, vector<int>(m, INT_MAX));\n        set<vector<int>>ms;\n        ms.insert({0, 0, 0});\n        while(!ms.empty()) {\n            auto top = *ms.begin();\n            ms.erase(ms.begin());\n            for (int k = 0; k < 4; k++) {\n                int ii = top[1] + di[k];\n                int jj = top[2] + dj[k];\n                if (!(ii >= 0 && ii < n && jj >= 0 && jj < m))\n                    continue;\n                if (grid[ii][jj] <= top[0] + 1) {\n                    if(t[ii][jj] > top[0] + 1) {\n                        t[ii][jj] = top[0] + 1;\n                        ms.insert({t[ii][jj], ii, jj});\n                    }\n                    continue;\n                }\n                int diff = grid[ii][jj] - top[0];\n                int min_ii_jj = diff % 2 == 0 ? grid[ii][jj] + 1 : grid[ii][jj];\n                if (t[ii][jj] > min_ii_jj) {\n                    t[ii][jj] = min_ii_jj;\n                    ms.insert({min_ii_jj, ii, jj});\n                }\n            }\n        }\n        return t[n-1][m-1];\n    }\n};\n```
['C++']