LeetCode試煉之路(4):牆與門(286)


題目的英文版是這樣的:
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

簡單翻譯成中文意思就是:-1所在的位置是牆,不能通過,0是門所在的位置,其他數字是可以通過的,要通過一個算法計算兩個門之間的最短距離。概括一下就是這個是迷宮最有解的問題。

解:
提示:
1 二維矩陣可以看成是一個二維數組
2 BFS(廣度優先算法)問題
思路:
這也是最常見的BFS的問題,需要不斷遍歷兩個門相鄰節點,然后判斷是否可以通過,如果通過則可以繼續對該點進行遍歷,然后每對一個點遍歷一次相鄰的節點,都需要記錄層數(這個層數就是距離);然后每次遍歷都需要將遍歷過的點用一個集合收集起來,以后遍歷需要避開這些點,因為越早遍歷到該點,說明用的距離是越短,所以第一次遍歷到的點就是最短距離。

public static void wallsAndGates(int[][] a){
    //先找到0所在位置(門所在位置)
    for(int n=0;n<a.length;n++){
        for(int m=0;m<a[0].length;m++){
            if(a[n][m]==0){
                int min = bfs(a,n,m);
                //System.out.println(min);
                if(min>0){
                    System.out.println(min);
                    return;
                }
            }
        }
    }
}
//遍歷從這個門到另一個門的最短距離
public static int bfs(int[][] a,int n,int m){
    Set<String> visits = new HashSet<String>();
    Queue<String> queue = new LinkedList<String>();
    queue.offer(n+""+m);
    visits.add(n+""+m);
    int count = 0;
    while(!queue.isEmpty()){
        int size = queue.size();
        for(int i=0;i<size;i++){
            String top = queue.poll();
            char[] temp = top.toCharArray();
            n = Integer.parseInt(""+temp[0]);
            m = Integer.parseInt(""+temp[1]);

// System.out.println(a[n][m]);
//遍歷該節點的相鄰節點,如果
if(n>0){
int tmp = getCount(a,visits,n-1,m);
if(tmp1){
count++;
return count;
}else if(tmp
2){
visits.add((n - 1) + "" + m);
queue.add((n - 1) + "" + m);
}
}
if(n<a.length-1){
int tmp = getCount(a,visits,n+1,m);
if(tmp1){
count++;
return count;
}else if(tmp
2){
visits.add((n+1) + "" + m);
queue.add((n+1)+""+m);
}
}
if(m>0){
int tmp = getCount(a,visits,n,(m-1));
if(tmp1){
count++;
return count;
}else if(tmp
2){
visits.add(n + "" + (m-1));
queue.add(n + "" + (m - 1));
}
}
if(m<a[0].length-1){
int tmp = getCount(a,visits,n,m+1);
if(tmp1){
count++;
return count;
}else if(tmp
2){
visits.add(n + "" + (m+1));
queue.add(n + "" + (m + 1));
}
}
}
count++;
}
return -1;
}

/**
 * 判斷是否已訪問的節點
 */
public static boolean findUsed(Set<String> visits,String visit){
    if(visits.contains(visit)){
        return true;
    }
    return false;
}

/**
 *
 */
public static int getCount(int[][] a,Set<String> visits,int n,int m){
    if(!findUsed(visits,n+""+m)) {
        visits.add(n + "" + m );
        if (a[n][m] == 0) {
            return 1;
        }else{
            return 2;
        }
    }
    return 0;
}

該方法效率不高,待優化!后續再優化更新


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM