python O(n) bfs & O(nlogn) sort
python\nclass Solution:\n    def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]:\n        # O(nlogn), sort\n        # return sorte
```python\nclass Solution:\n    def allCellsDistOrder(self, R: int, C: int, r0: int, c0: int) -> List[List[int]]:\n        # O(nlogn), sort\n        # return sorted([(a, b) for a in range(R) for b in range(C)], key = lambda x: abs(x[0] - r0) + abs(x[1] - c0))\n        \n        # O(n), BFS\n        dq = collections.deque([(r0, c0)])\n        res = [(r0, c0)]\n        seen = {(r0, c0)}\n        while dq:\n            sz = len(dq)\n            for _ in range(sz):\n                x, y = dq.popleft()\n                for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):\n                    xx, yy = x + dx, y + dy\n                    if 0 <= xx < R and 0 <= yy < C and (xx, yy) not in seen:\n                        seen.add((xx, yy))\n                        res.append((xx, yy))\n                        dq.append((xx, yy))\n        return res\n```
[]