You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter).

Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. You may return the answer in any order that satisfies this condition.

The distance between two cells (r1, c1) and (r2, c2) is |r1 - r2| + |c1 - c2|.

&nbsp;
Example 1:

Input: rows = 1, cols = 2, rCenter = 0, cCenter = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (0, 0) to other cells are: [0,1]


Example 2:

Input: rows = 2, cols = 2, rCenter = 0, cCenter = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (0, 1) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.


Example 3:

Input: rows = 2, cols = 3, rCenter = 1, cCenter = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (1, 2) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].


&nbsp;
Constraints:


	1 &lt;= rows, cols &lt;= 100
	0 &lt;= rCenter &lt; rows
	0 &lt;= cCenter &lt; cols


class Solution:
    def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]:
        # create a r, c matrix given the rows & cols
        # each element represents a list [r, c] where r is the row & c the col
        # find find the distances of all cells from the center (append to res)
        # sort the result by distance function
        # Time O(M + N) Space O(M + N)
        
        
        def distance(p1, p2):
            return abs(p1[0] - p2[0]) + abs(p1[1] - p2[1])
        
        matrix = [[i, j] for i in range(rows) for j in range(cols)]
        center = [rCenter, cCenter]
        matrix.sort(key=lambda c: distance(c, center))
        
        return matrix
//--------------------Method 1----------------------

class Solution {
    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {

        int [][]res=new int[rows*cols][2];

        int idx=0;

        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                res[idx][0]=i;
                res[idx][1]=j;
                idx++;
            }
        }

        Arrays.sort(res,(a,b)->{
            int d1=Math.abs(a[0]-rCenter)+Math.abs(a[1]-cCenter);
            int d2=Math.abs(b[0]-rCenter)+Math.abs(b[1]-cCenter);

            return d1-d2;
        });

        return res;
    }
}

//--------------------Method 2--------------------

// class Solution {
// public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {

// boolean [][]vis=new boolean[rows][cols];
// int [][]ans=new int[rows*cols][2];

// Queue<Pair> q=new LinkedList<>();
// q.add(new Pair(rCenter,cCenter));
// int idx=0;
// vis[rCenter][cCenter]=true;
// int [][]dir={{0,1},{1,0},{-1,0},{0,-1}};

// while(!q.isEmpty()){
// Pair curr=q.remove();
// ans[idx][0]=curr.r;
// ans[idx][1]=curr.c;
// idx++;

// for(int []d:dir){
// int nr=curr.r+d[0];
// int nc=curr.c+d[1];

// if(nr>=0 && nr<rows && nc>=0 && nc<cols && !vis[nr][nc]){
// vis[nr][nc]=true;
// q.add(new Pair(nr,nc));
// }
// }
// }

// return ans;
// }
// }

// class Pair{
// int r;
// int c;

// public Pair(int r, int c){
// this.r=r;
// this.c=c;
// }
// }
class Solution {
public:
    vector<vector<int>> allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
        vector<vector<int>> ans;
        multimap<int,vector<int>> mp;
        for(int i = 0;i<rows;i++){
            for(int j = 0;j<cols;j++){
                    mp.insert(pair<int,vector<int>>(abs(i-rCenter)+abs(j-cCenter),{i,j})); //map will sort the value based on key(distance)    
                }
            }
        for(auto itr = mp.begin();itr != mp.end();itr++){
            ans.push_back(itr->second);
        }
        return ans;
    }
};
var allCellsDistOrder = function(rows, cols, rCenter, cCenter) {
    let distances = []
    let result = []

    //create a new "visited" cells matrix
    let visited = new Array(rows).fill([])
    for(i=0;i<visited.length;i++){
        visited[i] = new Array(cols).fill(0)
    }

    const computeDistances = (row, col, rCenter, cCenter) => {
        if(row >= rows || col >= cols) return
        if(visited[row][col]) return // don't compute distance again if cell already visited
        visited[row][col] = 1

        let distance = Math.abs(rCenter - row) + Math.abs(cCenter - col)
        if(distances[distance]){
            distances[distance].add([row,col])
        }else{
            distances[distance] = new Set()
            distances[distance].add([row,col])
        }

        computeDistances(row + 1, col, rCenter, cCenter)
        computeDistances(row, col + 1, rCenter, cCenter)
    }

    computeDistances(0, 0, rCenter, cCenter)

    distances.forEach(distance => {
        result.push(...distance)
    })

    return result
};
Matrix Cells in Distance Order