美文网首页
130. Surrounded Regions

130. Surrounded Regions

作者: evil_ice | 来源:发表于2017-01-16 23:33 被阅读253次

题目130. Surrounded Regions

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
A region is captured by flipping all 'O's into 'X's in that surrounded region.
For example,
X X X X
X O O X
X X O X
X O X X
After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X

1,BFS+并查集

思路

1, 所有相连的'O'加入一个集合
    1)如果'O'被'X'包围,那么所有相连的'O'行程一个独立的集合
    2)如果相连的多个'O'最后在边界没有被'X'包围,那么他们都加入到一个孤立的isolateUnion中
2,将不在isolateUnion中的所有'O'置换为'X'
public class Solution {
    private int[] parents;
    public void initUnionFind(int m, int n, char[][] board){
        parents = new int[m*n + 1];
        for(int i=0; i<m; i++){
            int fullRowNum = i*n;
            for(int j=0; j<n; j++){
                parents[fullRowNum+j] = fullRowNum+j;
            }
        }
        parents[m*n] = m*n;
    }
    
    public int find(int idx){
        while(idx != parents[idx]){
            parents[idx] = parents[parents[idx]];
            idx = parents[idx];
        }
        return idx;
    }
    
    public void union(int p, int q){
        int pRoot = find(p);
        int qRoot = find(q);
        if(pRoot != qRoot){
            parents[qRoot] = pRoot;
        }
    }
    
    public boolean isConnected(int p, int q){
        return find(p) == find(q);
    }
    
    public void solve(char[][] board) {
        if(board == null || board.length == 0){
            return ;
        }
        int m = board.length;
        
        if(board[0] == null || board[0].length == 0){
            return ;
        }
        int n = board[0].length;
        
        initUnionFind(m,n,board);
        int isolateUnion = m*n;
        
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(board[i][j] == 'X'){
                    continue;
                }
                
                //下面是board[i][j] = 'O'
                int cur = i*n+j;
                if(i == 0 || j ==0 || i == m-1 || j == n-1){
                    union(cur, isolateUnion);
                }else{
                    if(i>0 && board[i-1][j] == 'O'){
                        union(cur,cur-n);
                    }
                    
                    if(i < m-1 && board[i+1][j] == 'O'){
                        union(cur,cur+n);
                    }
                    
                    if(j>0 && board[i][j-1] == 'O'){
                        union(cur,cur-1);
                    }
                    
                    if(j < n-1 && board[i][j+1] == 'O'){
                        union(cur,cur+1);
                    }
                }
            }
        }
        
        for(int i=0; i<m; i++){
            int fullRowNum = i*n;
            for(int j=0; j<n; j++){
                if(!isConnected(fullRowNum+j,isolateUnion)){
                    board[i][j] = 'X';
                }
            }
        }
    }
}

相关文章

网友评论

      本文标题:130. Surrounded Regions

      本文链接:https://www.haomeiwen.com/subject/zwzpbttx.html