Priority Queue Solution in java
\nclass Solution {\n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        \n        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)-
```\nclass Solution {\n    public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {\n        \n        PriorityQueue<int[]> pq = new PriorityQueue<>((a,b)->a[2]-b[2]);\n        \n        for(int r=0;r < R;r++){\n            for(int c=0;c < C;c++){\n                int dist = Math.abs(r-r0) + Math.abs(c-c0);\n                pq.offer(new int[]{r,c,dist});\n            }\n        }\n        int[][] res = new int[R*C][2];\n        int k = 0;\n        while(!pq.isEmpty()){\n            int[] val = pq.poll();\n            res[k][0] = val[0];\n            res[k][1] = val[1];\n            k++;\n        }\n        return res;\n    }\n}\n```\n\n
['Heap (Priority Queue)']