摘要:題目鏈接這道題感覺是那道的簡化版,思路都是一樣的。是把所有的點都先放到里面,然后一起遍歷。這種寫法的好處是保證了每個點都只被放進一次,不會重復遍歷。保證了時間復雜是。可以不寫成層次遍歷的形式,直接,的程序
Walls and Gates
題目鏈接:https://leetcode.com/problems...
這道題感覺是那道“Shortest Distance from All Buildings”的簡化版,思路都是一樣的。鏈接:https://segmentfault.com/a/11...
public class Solution { public void wallsAndGates(int[][] rooms) { /* bfs for each gates, room[i][j] = distance from (i, j) to gate * update room[i][j] each time * bfs: 1. qand level * 2. go over current level * if (x, y) is in matrix && empty && room[x][y] > level * - room[x][y] = level * - q.offer(x, y) */ 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); } } } int[] dx = new int[] {-1, 1, 0, 0}; int[] dy = new int[] {0, 0, -1, 1}; private void bfs(int[][] rooms, int x, int y) { Queue q = new LinkedList(); q.offer(new int[] {x, y}); int level = 0; // 1. bfs loop use a queue while(!q.isEmpty()) { level++; // 2. go over current level for(int j = q.size(); j > 0; j--) { int[] cur = q.poll(); // 4 directions for(int i = 0; i < 4; i++) { int nx = cur[0] + dx[i], ny = cur[1] + dy[i]; // if (x, y) is in matrix && empty && room[x][y] > level if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > level) { rooms[nx][ny] = level; q.offer(new int[] {nx, ny}); } } } } } }
看到discussion里面bfs還有一種寫法。是把所有gates的點都先放到q里面,然后一起遍歷。這種寫法的好處是:保證了每個點都只被放進q一次,不會重復遍歷。保證了時間復雜是O(MN), M = rooms.length, N = rooms[0].length。
public class Solution { public void wallsAndGates(int[][] rooms) { /* bfs */ Queueq = new LinkedList(); for(int i = 0; i < rooms.length; i++) { for(int j = 0; j < rooms[0].length; j++) { if(rooms[i][j] == 0) q.offer(new int[] {i, j}); } } bfs(rooms, q); } int[] dx = new int[] {-1, 1, 0, 0}; int[] dy = new int[] {0, 0, -1, 1}; private void bfs(int[][] rooms, Queue q) { int level = 0; while(!q.isEmpty()) { level++; for(int j = q.size(); j > 0; j--) { int[] cur = q.poll(); // 4 directions for(int i = 0; i < 4; i++) { int nx = cur[0] + dx[i], ny = cur[1] + dy[i]; if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > level) { rooms[nx][ny] = level; q.offer(new int[] {nx, ny}); } } } } } }
可以不寫成層次遍歷的形式,直接bfs,level = poll + 1:
public class Solution { public void wallsAndGates(int[][] rooms) { /* bfs */ Queueq = new LinkedList(); for(int i = 0; i < rooms.length; i++) { for(int j = 0; j < rooms[0].length; j++) { if(rooms[i][j] == 0) q.offer(new int[] {i, j}); } } bfs(rooms, q); } int[] dx = new int[] {-1, 1, 0, 0}; int[] dy = new int[] {0, 0, -1, 1}; private void bfs(int[][] rooms, Queue q) { while(!q.isEmpty()) { int[] cur = q.poll(); int x = cur[0], y = cur[1]; // 4 directions for(int i = 0; i < 4; i++) { int nx = cur[0] + dx[i], ny = cur[1] + dy[i]; if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > rooms[x][y] + 1) { rooms[nx][ny] = rooms[x][y] + 1; q.offer(new int[] {nx, ny}); } } } } }
dfs的程序:
public class Solution { public void wallsAndGates(int[][] rooms) { /* dfs */ 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); } } } int[] dx = new int[] {-1, 1, 0, 0}; int[] dy = new int[] {0, 0, -1, 1}; private void dfs(int[][] rooms, int x, int y, int depth) { for(int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if(nx >= 0 && nx < rooms.length && ny >= 0 && ny < rooms[0].length && rooms[nx][ny] > depth + 1) { rooms[nx][ny] = depth + 1; dfs(rooms, nx, ny, depth + 1); } } } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/66555.html
摘要:廣度優先搜索復雜度時間空間思路實際上就是找每個房間到最近的門的距離,我們從每個門開始,廣度優先搜索并記錄層數就行了。這題要注意剪枝,如果下一步是門或者下一步是墻或者下一步已經訪問過了,就不要加入隊列中。 Walls and Gates You are given a m x n 2D grid initialized with these three possible values....
摘要:題目解答每一次加入進來的結點,都時當前位置可到達的最短距離。 題目: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...
摘要:本人郵箱歡迎轉載轉載請注明網址代碼已經全部托管有需要的同學自行下載引言迷宮對于大家都不會陌生那么迷宮是怎么生成已經迷宮要如何找到正確的路徑呢用代碼又怎么實現帶著這些問題我們繼續往下看并查集朋友圈有一種算法就做并查集什么意思呢比如現在有零 本人郵箱: 歡迎轉載,轉載請注明網址 http://blog.csdn.net/tianshi_kcogithub: https://github.c...
閱讀 1010·2021-11-22 13:52
閱讀 924·2019-08-30 15:44
閱讀 570·2019-08-30 15:43
閱讀 2424·2019-08-30 12:52
閱讀 3473·2019-08-29 16:16
閱讀 637·2019-08-29 13:05
閱讀 2943·2019-08-26 18:36
閱讀 1975·2019-08-26 13:46