286 walls and gate最近的出口
作者:互联网
参考:
public void wallsAndGates(int[][] rooms) { for (int i = 0; i < rooms.length; i++) for (int j = 0; j < rooms[0].length; j++) if (rooms[i][j] == 0) dfs(rooms, i, j, 0); } private void dfs(int[][] rooms, int i, int j, int d) { if (i < 0 || i >= rooms.length || j < 0 || j >= rooms[0].length || rooms[i][j] < d) return; rooms[i][j] = d; dfs(rooms, i - 1, j, d + 1); dfs(rooms, i + 1, j, d + 1); dfs(rooms, i, j - 1, d + 1); dfs(rooms, i, j + 1, d + 1); }
标签:int,void,length,dfs,++,walls,rooms,gate,286 来源: https://www.cnblogs.com/immiao0319/p/14881757.html