摘要:題目鏈接這道題感覺是那道的簡化版,思路都是一樣的。是把所有的點(diǎn)都先放到里面,然后一起遍歷。這種寫法的好處是保證了每個點(diǎn)都只被放進(jìn)一次,不會重復(fù)遍歷。保證了時間復(fù)雜是。可以不寫成層次遍歷的形式,直接,的程序
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還有一種寫法。是把所有g(shù)ates的點(diǎn)都先放到q里面,然后一起遍歷。這種寫法的好處是:保證了每個點(diǎn)都只被放進(jìn)q一次,不會重復(fù)遍歷。保證了時間復(fù)雜是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); } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/66555.html
摘要:廣度優(yōu)先搜索復(fù)雜度時間空間思路實(shí)際上就是找每個房間到最近的門的距離,我們從每個門開始,廣度優(yōu)先搜索并記錄層數(shù)就行了。這題要注意剪枝,如果下一步是門或者下一步是墻或者下一步已經(jīng)訪問過了,就不要加入隊列中。 Walls and Gates You are given a m x n 2D grid initialized with these three possible values....
摘要:題目解答每一次加入進(jìn)來的結(jié)點(diǎn),都時當(dāng)前位置可到達(dá)的最短距離。 題目: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...
摘要:本人郵箱歡迎轉(zhuǎn)載轉(zhuǎn)載請注明網(wǎng)址代碼已經(jīng)全部托管有需要的同學(xué)自行下載引言迷宮對于大家都不會陌生那么迷宮是怎么生成已經(jīng)迷宮要如何找到正確的路徑呢用代碼又怎么實(shí)現(xiàn)帶著這些問題我們繼續(xù)往下看并查集朋友圈有一種算法就做并查集什么意思呢比如現(xiàn)在有零 本人郵箱: 歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明網(wǎng)址 http://blog.csdn.net/tianshi_kcogithub: https://github.c...
閱讀 1026·2021-11-22 13:52
閱讀 939·2019-08-30 15:44
閱讀 580·2019-08-30 15:43
閱讀 2437·2019-08-30 12:52
閱讀 3486·2019-08-29 16:16
閱讀 647·2019-08-29 13:05
閱讀 2952·2019-08-26 18:36
閱讀 2007·2019-08-26 13:46