Matrix Cells in Distance Order | Java | Easy | Queue | BFS | Sorting | 2 methods
\n//--------------------Method 1----------------------\n\nclass Solution {\n    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {
```\n//--------------------Method 1----------------------\n\nclass Solution {\n    public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {\n        \n        int [][]res=new int[rows*cols][2];\n        \n        int idx=0;\n        \n        for(int i=0;i<rows;i++){\n            for(int j=0;j<cols;j++){\n                res[idx][0]=i;\n                res[idx][1]=j;\n                idx++;\n            }\n        }\n        \n        Arrays.sort(res,(a,b)->{\n            int d1=Math.abs(a[0]-rCenter)+Math.abs(a[1]-cCenter);\n            int d2=Math.abs(b[0]-rCenter)+Math.abs(b[1]-cCenter);\n            \n            return d1-d2;\n        });\n        \n        return res;\n    }\n}\n\n//--------------------Method 2--------------------\n\n// class Solution {\n//     public int[][] allCellsDistOrder(int rows, int cols, int rCenter, int cCenter) {\n        \n//         boolean [][]vis=new boolean[rows][cols];\n//         int [][]ans=new int[rows*cols][2];\n        \n//         Queue<Pair> q=new LinkedList<>();\n//         q.add(new Pair(rCenter,cCenter));\n//         int idx=0;\n//         vis[rCenter][cCenter]=true;\n//         int [][]dir={{0,1},{1,0},{-1,0},{0,-1}};\n        \n//         while(!q.isEmpty()){\n//             Pair curr=q.remove();\n//             ans[idx][0]=curr.r;\n//             ans[idx][1]=curr.c;\n//             idx++;\n            \n//             for(int []d:dir){\n//                 int nr=curr.r+d[0];\n//                 int nc=curr.c+d[1];\n                \n//                 if(nr>=0 && nr<rows && nc>=0 && nc<cols && !vis[nr][nc]){\n//                     vis[nr][nc]=true;\n//                     q.add(new Pair(nr,nc));\n//                 }\n//             }\n//         }\n        \n//         return ans;\n//     }\n// }\n\n// class Pair{\n//     int r;\n//     int c;\n    \n//     public Pair(int r, int c){\n//         this.r=r;\n//         this.c=c;\n//     }\n// }\n```
['Breadth-First Search', 'Queue', 'Java']