【leetcode-41】【DFS / BFS / 並查集】130.被圍繞的區域

題目描述

給定一個二維的矩陣,包含 ‘X’ 和 ‘O’(字母 O)。

找到所有被 ‘X’ 圍繞的區域,並將這些區域裏所有的 ‘O’ 用 ‘X’ 填充。

示例:

X X X X
X O O X
X X O X
X O X X
運行你的函數後,矩陣變爲:

X X X X
X X X X
X X X X
X O X X
解釋:

被圍繞的區間不會存在於邊界上,換句話說,任何邊界上的 ‘O’ 都不會被填充爲 ‘X’。 任何不在邊界上,或不與邊界上的 ‘O’ 相連的 ‘O’ 最終都會被填充爲 ‘X’。如果兩個元素在水平或垂直方向相鄰,則稱它們是“相連”的。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/surrounded-regions
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

解題思路

  • 共有三種解題思路,dfs、bfs、並查集
  • 本題的關鍵在於從邊界開始,而不是通常的遍歷順序,從邊界開始,碰到o就進行一些措施。
  • 二位數組的dfs也好,bfs也好,他的dfs遞歸或者bfs的周邊都是(i-1, j),(i+1, j),(i, j+1),(i, j-1)對上述數據進行訪問,dfs和bfs都是先把和邊界相連的o字符換爲#,最後再遍歷一次,把o換爲x,把#換爲o。
  • dfs遞歸結束的條件是碰到#或者x,或者i,j座標無效
  • bfs就是套模板,把周圍的數據都添加到隊列裏,然後從隊列往出poll,這個過程叫傳染函數???
  • 並查集的辦法非常巧妙,給各位看官指明一條名路渣東教你用並查集

代碼

  • dfs
class Solution {
    Set<Integer> visited;
    public void solve(char[][] board) {
        if(board == null || board.length == 0) return;
        visited = new HashSet<>();
        int col = board[0].length;
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                if((i == 0 || j == 0 || i == board.length - 1 || j == board[0].length - 1) && board[i][j] == 'O'){
                    dfs(board, i, j);
                }
            }
        }
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                } else if(board[i][j] == '#'){
                    board[i][j] = 'O';
                }
            }
        }
    }
    public void dfs(char[][] board, int i, int j) {
        if(i < 0 || j < 0 || i == board.length || j == board[0].length) return;
        if(board[i][j] == '#' || board[i][j] == 'X') return;
        board[i][j] = '#';
        dfs(board, i-1, j);
        dfs(board, i+1, j);
        dfs(board, i, j+1);
        dfs(board, i, j-1);
    }
}
  • bfs
class Solution {
    Set<Integer> visited;
    public void solve(char[][] board) {
        if(board == null || board.length == 0) return;
        visited = new HashSet<>();
        Queue<Integer> queue = new LinkedList<>();
        int col = board[0].length, row = board.length;
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                if((i == 0 || j == 0 || i == board.length - 1 || j == board[0].length - 1) && board[i][j] == 'O'){
                    queue.offer(i * col + j);
                    while(!queue.isEmpty()){
                        int sz = queue.size();
                        while(sz-- > 0){
                            int sum = queue.poll();
                            int nr = sum / col, nc = sum % col;
                            board[nr][nc] = '#';
                            if(nr > 0 && board[nr-1][nc] == 'O') queue.offer((nr-1) * col + nc);
                            if(nr < row - 1 && board[nr+1][nc] == 'O') queue.offer((nr+1) * col + nc);
                            if(nc > 0 && board[nr][nc-1] == 'O') queue.offer(nr * col + nc - 1);
                            if(nc < col - 1 && board[nr][nc+1] == 'O') queue.offer(nr * col + nc + 1);
                        }
                    }
                }
            }
        }
        for(int i = 0; i < board.length; i++){
            for(int j = 0; j < board[0].length; j++){
                if(board[i][j] == 'O'){
                    board[i][j] = 'X';
                } else if(board[i][j] == '#'){
                    board[i][j] = 'O';
                }
            }
        }
    }
}

並查集

class Solution {
    class UF{
        private int[] parent;
        private int[] size;
        private int count;
        public UF(int n){
            parent = new int[n];
            size = new int[n];
            count = n;
            for(int i = 0; i < n; i++){
                parent[i] = i;
                size[i] = 1;
            }
        }
        public int find(int x){
            while(parent[x] != x){
                parent[x] = parent[parent[x]];
                x = parent[x];
            }
            return x;
        }
        public void union(int x, int y){
            int par1 = find(x);
            int par2 = find(y);
            if(par2 == par1) return;
            count--;
            if(size[par1] > size[par2]){
                parent[par2] = par1;
                size[par2] += size[par1];
            } else {
                parent[par1] = par2;
                size[par1] += size[par2];
            }
        }
        public boolean isConnected(int x, int y){
            int par1 = find(x);
            int par2 = find(y);
            return par2 == par1;
        }
    }
    public void solve(char[][] board) {
        if(board == null || board.length == 0) return;
        int col = board[0].length, row = board.length;
        UF uf = new UF(col * row + 1);
        int dummy = col*row;
        for(int i = 0; i < row; i++){
            if(board[i][0] == 'O') uf.union(i*col, dummy);
            if(board[i][col-1] == 'O') uf.union(i * col + col - 1, dummy);
        }
        for(int i = 0; i < col; i++){
            if(board[0][i] == 'O') uf.union(i, dummy);
            if(board[row-1][i] == 'O') uf.union((row-1)*col+i, dummy);
        }
        int[][] d = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        for(int i = 1; i < row - 1; i++){
            for(int j = 1; j < col - 1; j++){
                if(board[i][j] == 'O'){
                    for(int k = 0; k < 4; k++){
                        int x = i + d[k][0];
                        int y = j + d[k][1];
                        if(board[x][y] == 'O') uf.union(x*col+y, i*col+j);
                    }
                }
            }
        }
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if(board[i][j] == 'O' && !uf.isConnected(i * col + j, dummy)){
                    board[i][j] = 'X';
                }
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章