286 walls and gate最近的出口
Posted 排序和map
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
以上是关于286 walls and gate最近的出口的主要内容,如果未能解决你的问题,请参考以下文章