[JavaScript] Easy to understand - 2 solutions - 132ms
The first solution is do it by counting sort. Here\'s the algorithm:\n\n1. Traversal the whole matrix, calculate the distance for each cell and save them into a
The first solution is do it by counting sort. Here\'s the algorithm:\n\n1. Traversal the whole matrix, calculate the distance for each cell and save them into a list whose index could be used as the distance value.\n2. Merge all items in the list and you will get the answer.\n\n```js\nconst allCellsDistOrder = (r, c, r0, c0) => {\n  const buckets = [];\n  const ret = [];\n  for (let i = 0; i < r; ++i) {\n    for (let j = 0; j < c; ++j) {\n      const dis = Math.abs(i - r0) + Math.abs(j - c0);\n      if (buckets[dis] === undefined) buckets[dis] = [];\n      buckets[dis].push([i, j]);\n    }\n  }\n  for (const bucket of buckets) {\n    ret.push(...bucket);\n  }\n  return ret;\n};\n```\n\nThe second solution is based on BFS. Here\'s the algorithm:\n\n1. Traversal the whole matrix from the `(r0, c0)` cell with a queue by BFS.\n2. Do some validation for each cell and you will get the answer.\n\n```js\nconst allCellsDistOrder = (r, c, r0, c0) => {\n  const visited = new Set();\n  const ret = [];\n  const queue = [[r0, c0]];\n  while (queue.length) {\n    const [x, y] = queue.shift();\n    if (x > r - 1 || x < 0 || y > c -1 || y < 0 || visited.has(x * 100 + y)) continue;\n    ret.push([x, y]);\n    visited.add(x * 100 + y);\n    [[0, -1], [0, 1], [1, 0], [-1, 0]].forEach(move => {\n      queue.push([x + move[0], y + move[1]]);\n    });\n  }\n  return ret;\n};\n```
['JavaScript']