摘要:广度优先搜索复杂度时间空间思路实际上就是找每个房间到最近的门的距离,我们从每个门开始,广度优先搜索并记录层数就行了。这题要注意剪枝,如果下一步是门或者下一步是墙或者下一步已经访问过了,就不要加入队列中。
Walls and Gates
广度优先搜索 复杂度You are given a m x n 2D grid initialized with these three possible values.
-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647. Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.
For example, given the 2D grid:
INF -1 0 INF INF INF INF -1 INF -1 INF -1 0 -1 INF INFAfter running your function, the 2D grid should be:
3 -1 0 1 2 2 1 -1 1 -1 2 -1 0 -1 3 4
时间 O(NM) 空间 O(N)
思路实际上就是找每个房间到最近的门的距离,我们从每个门开始,广度优先搜索并记录层数就行了。如果某个房间之前被标记过距离,那就选择这个距离和当前距离中较小的那个。这题要注意剪枝,如果下一步是门或者下一步是墙或者下一步已经访问过了,就不要加入队列中。否则会超时。
代码public class Solution { public void wallsAndGates(int[][] rooms) { if(rooms.length == 0) return; for(int i = 0; i < rooms.length; i++){ for(int j = 0; j < rooms[0].length; j++){ // 如果遇到一个门,从门开始广度优先搜索,标记连通的节点到自己的距离 if(rooms[i][j] == 0) bfs(rooms, i, j); } } } public void bfs(int[][] rooms, int i, int j){ Queuequeue = new LinkedList (); queue.offer(i * rooms[0].length + j); int dist = 0; // 用一个集合记录已经访问过的点 Set visited = new HashSet (); visited.add(i * rooms[0].length + j); while(!queue.isEmpty()){ int size = queue.size(); // 记录深度的搜索 for(int k = 0; k < size; k++){ Integer curr = queue.poll(); int row = curr / rooms[0].length; int col = curr % rooms[0].length; // 选取之前标记的值和当前的距离的较小值 rooms[row][col] = Math.min(rooms[row][col], dist); int up = (row - 1) * rooms[0].length + col; int down = (row + 1) * rooms[0].length + col; int left = row * rooms[0].length + col - 1; int right = row * rooms[0].length + col + 1; if(row > 0 && rooms[row - 1][col] > 0 && !visited.contains(up)){ queue.offer(up); visited.add(up); } if(col > 0 && rooms[row][col - 1] > 0 && !visited.contains(left)){ queue.offer(left); visited.add(left); } if(row < rooms.length - 1 && rooms[row + 1][col] > 0 && !visited.contains(down)){ queue.offer(down); visited.add(down); } if(col < rooms[0].length - 1 && rooms[row][col + 1] > 0 && !visited.contains(right)){ queue.offer(right); visited.add(right); } } dist++; } } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/64721.html
摘要:题目链接这道题感觉是那道的简化版,思路都是一样的。是把所有的点都先放到里面,然后一起遍历。这种写法的好处是保证了每个点都只被放进一次,不会重复遍历。保证了时间复杂是。可以不写成层次遍历的形式,直接,的程序 Walls and Gates 题目链接:https://leetcode.com/problems... 这道题感觉是那道Shortest Distance from All Bu...
摘要:题目解答每一次加入进来的结点,都时当前位置可到达的最短距离。 题目:You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle.0 - A gate.INF - Infinity means an empty room. We use the...
Problem There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it wont stop rolling until hitting a wall. When the ball sto...
阅读 1545·2021-10-25 09:44
阅读 2912·2021-09-04 16:48
阅读 1498·2019-08-30 15:44
阅读 2378·2019-08-30 15:44
阅读 1711·2019-08-30 15:44
阅读 2797·2019-08-30 14:14
阅读 2930·2019-08-30 13:00
阅读 2104·2019-08-30 11:09