网站建设的基础,建筑图纸字母代表大全图解,全媒体运营师培训机构,网站设计就业前景给出 R 行 C 列的矩阵#xff0c;其中的单元格的整数坐标为 (r, c)#xff0c;满足 0 r R 且 0 c C。
另外#xff0c;我们在该矩阵中给出了一个坐标为 (r0, c0) 的单元格。
返回矩阵中的所有单元格的坐标#xff0c;并按到 (r0, c0) 的距离从最小到…给出 R 行 C 列的矩阵其中的单元格的整数坐标为 (r, c)满足 0 r R 且 0 c C。
另外我们在该矩阵中给出了一个坐标为 (r0, c0) 的单元格。
返回矩阵中的所有单元格的坐标并按到 (r0, c0) 的距离从最小到最大的顺序排其中两单元格(r1, c1) 和 (r2, c2) 之间的距离是曼哈顿距离|r1 - r2| |c1 - c2|。你可以按任何满足此条件的顺序返回答案。
示例 1
输入R 1, C 2, r0 0, c0 0 输出[[0,0],[0,1]] 解释从 (r0, c0) 到其他单元格的距离为[0,1]
代码
class Solution {public int[][] allCellsDistOrder(int R, int C, int r0, int c0) {boolean [][] checknew boolean[R][C];check[r0][c0]true;LinkedListint[] resnew LinkedList();int[][] dirnew int[][]{{0,1},{1,0},{-1,0},{0,-1}};Queueint[] queuenew LinkedList();queue.add(new int[]{r0,c0});while (!queue.isEmpty())//广度优先搜索{int sizequeue.size();for(int i0;isize;i){int[] curqueue.poll();res.add(cur);int xcur[0],ycur[1];for(int[] d:dir){int nextXd[0]x,nextYd[1]y;if(nextX0nextXRnextY0nextYC!check[nextX][nextY]){check[nextX][nextY]true;queue.add(new int[]{nextX,nextY});}}}}return res.toArray(new int[res.size()][2]);}
}