O(n) with picture
Solution\n1. Increment the distance from 0 till R + C.\n2. From our point, generate all points with the distance.\n3. Add valid poits (within our grid) to the r
# Solution\n1. Increment the distance from 0 till ```R + C```.\n2. From our point, generate all points with the distance.\n3. Add valid poits (within our grid) to the result.\n\nThe image below demonstrates the generation of points with distance 5:\n![image](https://assets.leetcode.com/users/votrubac/image_1555825343.png)\n```\nvector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {\n  vector<vector<int>> res = { { r0, c0 } };\n  auto max_d = max({ r0 + c0, c0 + R - r0, r0 + C - c0, R - r0 + C - c0 });\n  for (auto d = 1; d <= max_d; ++d)\n    for (int x = d; x >= -d; --x) {\n      auto r1 = r0 + x, c1_a = c0 + d - abs(x), c1_b = c0 + abs(x) - d;\n      if (r1 >= 0 && r1 < R) {\n        if (c1_a >= 0 && c1_a < C) \n            res.push_back({ r1, c1_a });\n        if (c1_a != c1_b && c1_b >= 0 && c1_b < C) \n            res.push_back({ r1, c1_b });\n      }\n    }\n  return res;\n}\n```\n# Complexity Analysis\nRuntime: *O((R + C) ^ 2)*, which is linear to the total number of cells. We analyze maximum 4(R + C - 2)(R + C - 2) cells once.\nMemory: *O(1)*. We do not use any additional memory (except for returning the result, which does not count).
[]