C++ O(RC) time, O(1) additional space
Idea: start from (r0, c0) and add elements around it to the answer vector layer by layer, each layer having a diamond shape as in the below picture.\n\n\n\n\n\n
Idea: start from (r0, c0) and add elements around it to the answer vector layer by layer, each layer having a diamond shape as in the below picture.\n\n\n![image](https://assets.leetcode.com/users/images/fb7d22e7-e109-4723-94a5-4382f377f5f2_1594039822.4483232.png)\n\n\n```\n     vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {\n        vector<vector<int>> ans;\n        int l = 1, x = r0, y = c0;\n        \n        auto diag = [&ans, R, C, &l, &x, &y] (int dir_row, int dir_col) {\n            for (int j = 1; j <= l; j++) {\n                x += dir_row, y += dir_col;\n                if (0 <= x && x < R && 0 <= y && y < C)\n                    ans.push_back({x, y});\n            }\n        };\n        \n        ans.push_back({x, y});\n        while (l < R + C) {\n            x = r0 - l; y = c0;\n            diag(1,-1); diag(1,1); diag(-1,1); diag(-1,-1);\n            l++;\n        }\n        \n        return ans;\n    }\n```
[]