✅C++✅C#✅JavaScript.🔥Fast and Explained solutions.💯
1.Create a list of all cells in the matrix, sorted by their distance from the given center cell. To do this, we can calculate the distance of each cell from the
# 1.Create a list of all cells in the matrix, sorted by their distance from the given center cell. To do this, we can calculate the distance of each cell from the center cell using the Manhattan distance formula: |r1 - r2| + |c1 - c2|.\n# 2.\n# Sort the list of cells based on their distance from the center cell.\n\n# 3.Return the sorted list of cells.\n\nTo implement this approach, we can use a two-dimensional array or a vector of pairs to store the coordinates of each cell in the matrix. We can then calculate the distance of each cell from the center cell using the Manhattan distance formula and store the distance along with the cell coordinates in a vector of pairs. Finally, we can sort the vector of pairs based on the distance and return the sorted list of cell coordinates. Alternatively, we can use a priority queue to store the cell coordinates sorted by distance and return the sorted list of cell coordinates .\n\n```javascript []\nvar allCellsDistOrder = function(rows, cols, rCenter, cCenter) {\n  const buckets = [];\n  const result = [];\n  const maxDist = Math.max(rCenter, rows - 1 - rCenter) + Math.max(cCenter, cols - 1 - cCenter);\n\n  for (let i = 0; i <= maxDist; i++) {\n    buckets.push([]);\n  }\n \n  for (let r = 0; r < rows; r++) {\n    for (let c = 0; c < cols; c++) {\n      const dist = Math.abs(r - rCenter) + Math.abs(c - cCenter);\n      buckets[dist].push([r, c]);\n    }\n  }\n\n  for (let i = 0; i <= maxDist; i++) {\n    result.push(...buckets[i]);\n  }\n  \n  return result;\n};\n```\n```C++ []\nclass Solution {\npublic:\n  vector<vector<int>> allCellsDistOrder(int rows, int cols, int rc, int cc) {\n    vector<vector<char>> vis(rows, vector<char>(cols, false));\n    vector<vector<int>> dists;\n    queue<pair<int, int>> q;\n    q.push({rc, cc});\n    vis[rc][cc] = true;\n    dists.push_back({rc, cc});\n\n    while (!q.empty()) {\n      auto [x, y] = q.front();\n      q.pop();\n      for (int i = 0; i < 4; ++i) {\n        int nx = x + dX[i];\n        int ny = y + dY[i];\n        if (nx < 0 || nx >= rows || ny < 0 || ny >= cols || vis[nx][ny]) continue;\n        q.push({nx, ny});\n        vis[nx][ny] = true;\n        dists.push_back({nx, ny});\n      }\n    }\n\n    return dists;\n  }\nprivate:\n  const int dX[4] = {-1, 0, 1, 0};\n  const int dY[4] = {0, 1, 0, -1};\n};\n```\n```C# []\npublic class Solution {\n    private readonly int[] dX = {-1, 0, 1, 0};\n    private readonly int[] dY = {0, 1, 0, -1};\n    \n    public int[][] AllCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {\n        var vis = new bool[rows][];\n        for (int i = 0; i < rows; i++) {\n            vis[i] = new bool[cols];\n        }\n        \n        var dists = new List<int[]>();\n        var q = new Queue<int[]>();\n        q.Enqueue(new int[] {rCenter, cCenter});\n        vis[rCenter][cCenter] = true;\n        dists.Add(new int[] {rCenter, cCenter});\n\n        while (q.Count > 0) {\n            var curr = q.Dequeue();\n            int x = curr[0], y = curr[1];\n            for (int i = 0; i < 4; ++i) {\n                int nx = x + dX[i];\n                int ny = y + dY[i];\n                if (nx < 0 || nx >= rows) continue;\n                if (ny < 0 || ny >= cols) continue;\n                if (vis[nx][ny]) continue;\n                q.Enqueue(new int[] {nx, ny});\n                vis[nx][ny] = true;\n                dists.Add(new int[] {nx, ny});\n            }\n        }\n\n        return dists.ToArray();\n    }\n}\n```\n\n\n
['C++', 'JavaScript', 'C#']