成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

[Leetcode] Walls and Gates 墻與門(mén)

Edison / 757人閱讀

摘要:廣度優(yōu)先搜索復(fù)雜度時(shí)間空間思路實(shí)際上就是找每個(gè)房間到最近的門(mén)的距離,我們從每個(gè)門(mén)開(kāi)始,廣度優(yōu)先搜索并記錄層數(shù)就行了。這題要注意剪枝,如果下一步是門(mén)或者下一步是墻或者下一步已經(jīng)訪問(wèn)過(guò)了,就不要加入隊(duì)列中。

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 INF 

After running your function, the 2D grid should be:

  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4
廣度優(yōu)先搜索 復(fù)雜度

時(shí)間 O(NM) 空間 O(N)

思路

實(shí)際上就是找每個(gè)房間到最近的門(mén)的距離,我們從每個(gè)門(mén)開(kāi)始,廣度優(yōu)先搜索并記錄層數(shù)就行了。如果某個(gè)房間之前被標(biāo)記過(guò)距離,那就選擇這個(gè)距離和當(dāng)前距離中較小的那個(gè)。這題要注意剪枝,如果下一步是門(mén)或者下一步是墻或者下一步已經(jīng)訪問(wèn)過(guò)了,就不要加入隊(duì)列中。否則會(huì)超時(shí)。

代碼
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++){
                // 如果遇到一個(gè)門(mén),從門(mén)開(kāi)始廣度優(yōu)先搜索,標(biāo)記連通的節(jié)點(diǎn)到自己的距離
                if(rooms[i][j] == 0) bfs(rooms, i, j);
            }
        }
    }
    
    public void bfs(int[][] rooms, int i, int j){
        Queue queue = new LinkedList();
        queue.offer(i * rooms[0].length + j);
        int dist = 0;
        // 用一個(gè)集合記錄已經(jīng)訪問(wèn)過(guò)的點(diǎn)
        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;
                // 選取之前標(biāo)記的值和當(dāng)前的距離的較小值
                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++;
        }
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/64721.html

相關(guān)文章

  • Walls and Gates

    摘要:題目鏈接這道題感覺(jué)是那道的簡(jiǎn)化版,思路都是一樣的。是把所有的點(diǎn)都先放到里面,然后一起遍歷。這種寫(xiě)法的好處是保證了每個(gè)點(diǎn)都只被放進(jìn)一次,不會(huì)重復(fù)遍歷。保證了時(shí)間復(fù)雜是。可以不寫(xiě)成層次遍歷的形式,直接,的程序 Walls and Gates 題目鏈接:https://leetcode.com/problems... 這道題感覺(jué)是那道Shortest Distance from All Bu...

    CKJOKER 評(píng)論0 收藏0
  • 286. Walls and Gates

    摘要:題目解答每一次加入進(jìn)來(lái)的結(jié)點(diǎn),都時(shí)當(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...

    megatron 評(píng)論0 收藏0
  • [LeetCode] 490. The Maze (BFS/DFS)

    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...

    smartlion 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<