130. Surrounded Regions 将包围的符号变换 BFS & DFS & UNION find

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

class Solution {
public:
    //1.用深度搜索的方法
    void mark(vector>& board, int i, int j, int m, int n){
        if(i > 1 && board[i-1][j] == 'O'){
            board[i-1][j] = '1';
            mark(board, i-1, j, m, n);
        }
        if(i+1 < m && board[i+1][j] == 'O'){
            board[i+1][j] = '1';
            mark(board, i+1, j, m, n);
        }
        if(j > 1 && board[i][j-1] == 'O'){
            board[i][j-1] = '1';
            mark(board, i, j-1, m, n);
        }
        if(j+1 < n && board[i][j+1] == 'O'){
            board[i][j+1] = '1';
            mark(board, i, j+1, m, n);
        }
        
    }
    
    void solve(vector>& board) {
        int m = board.size();
        if(m == 0) return;
        int n = board[0].size();
        if(n == 0) return;
        for(int i = 0; i < n; i++){ //对头尾两行进行边缘‘O’的查找
            if(board[0][i] == 'O'){
                board[0][i] = '1';
                mark(board, 0, i, m, n);
            }
            if(m > 1){
                if(board[m-1][i] == 'O'){
                    board[m-1][i] = '1';
                    mark(board, m-1, i, m, n);
                }
            }
        }
        
        for(int j = 0; j < m; j++){//对头尾两列进行边缘‘O’的查找
            if(board[j][0] == 'O'){
                board[j][0] = '1';
                mark(board, j, 0, m, n);
            }
            if(n > 1){
                if(board[j][n-1] == 'O'){
                    board[j][n-1] = '1';
                    mark(board, j, n-1, m, n);
                }
            }
        }
        
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
            }
        }
        
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == '1')
                    board[i][j] = 'O';
            }
        }
    }
};


2.BFS

class Solution {
public:

    void bfs(vector>& board, int x, int y, int m, int n){
        queue>q;
        q.push(make_pair(x,y));
        board[x][y] = '+';
        
        while(!q.empty()){
            paircur = q.front();
            q.pop();
            pairpos[4] = {{cur.first-1, cur.second}, {cur.first+1, cur.second},{cur.first, cur.second+1}, {cur.first, cur.second-1}};
            for(int i = 0;i < 4; i++){
                int xx = pos[i].first;
                int yy = pos[i].second;
                if(xx >= 0 && xx < m && yy >= 0 && yy < n && board[xx][yy] == 'O'){
                    q.push(pos[i]);
                    board[xx][yy] = '+';
                }
            }
        }
    }
    
    void solve(vector>& board) {
        //BFS
        int m = board.size();
        if(m == 0) return;
        int n = board[0].size();
        if(n == 0) return;
        for(int i = 0; i < n; i++){
            if(board[0][i] == 'O')
                bfs(board, 0, i, m, n);
            if(board[m-1][i] == 'O')
                bfs(board, m-1, i, m, n);
        }
        for(int i = 0; i < m; i++){
            if(board[i][0] == 'O')
                bfs(board, i, 0, m, n);
            if(board[i][n-1] == 'O')
                bfs(board, i, n-1, m, n);
        }
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == 'O')
                board[i][j] = 'X';
            }
        }
        
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == '+')
                    board[i][j] = 'O';
            }
        }
    }
};

3.union find


别人的思想;

并查集常用来解决连通性的问题,即将一个图中连通的部分划分出来。当我们判断图中两个点之间是否存在路径时,就可以根据判断他们是否在一个连通区域。 
当数据较多时,并查集比BFS和DFS算法适用。 
但是,并查集的缺陷就是不能够求出连通的两个点之间的路径,但是DFS算法或者BFS算法适用。 
并查集的思想就是,同一个连通区域内的所有点的根节点是同一个。将每个点映射成一个数字。先假设每个点的根节点就是他们自己,然后我们以此输入连通的点对,然后将其中一个点的根节点赋成另一个节点的根节点,这样这两个点所在连通区域又相互连通了。 
并查集的主要操作有:

  • find(int m):这是并查集的基本操作,查找m的根节点。
  • checkConnection(int m,int n):判断m,n两个点是否在一个连通区域。
  • Union(int m,int n):合并m,n两个点所在的连通区域。

当然,并查集还有较多优化,比如怎么避免并查集的树退化成一条链。合并的时候,应该尽量让小树根节点连在大树根节点上,尽量使得整个树扁平。

代码:

class Solution {
    //union find主要找根节点,union 主要对两两(右下)进行合并,同时若一个是边缘的,则使另一个也为边缘
private:
    vectorroot;
    vectorisedge;
public:
    void unions(int x, int y){
        int root_x = find(x);
        int root_y = find(y);
        if(root_x == root_y) return;
        root[root_x] = root_y; //将root_x 作为root_y的子节点
        if(isedge[root_x]) isedge[root_y] = true;
    }

    int find(int x){
        while(root[x] != x){
            root[x] = root[root[x]];
            x = root[x];
        }
        return x;
    }

    void solve(vector>& board) {
        if(board.size() == 0 || board[0].size() == 0) return;
        int height = board.size(), width = board[0].size();
        int n = height* width;
        root = vector(n, 0);
        isedge = vector(n, false);
        //1. 初始化root和isedge数组
        for(int i = 0; i < n; i++){
            int x = i / width, y = i % width;
            root[i] = i;
            if((board[x][y] == 'O') && ((x == 0) || (x == height-1) || (y == 0) || (y == width-1))) isedge[i] = true;
        }
        //2.union 
        for(int i = 0; i < n; i++){
            int x = i/width, y = i % width;
            if((x < height -1) && (board[x][y] == board[x+1][y])) unions(i, i + width); //跟下面的进行合并
            if((y < width -1) && (board[x][y] == board[x][y+1])) unions(i, i+1); 
        }
        //3.find
        for(int i = 0; i < n; i++){
            int x = i/width, y = i%width;
            if(board[x][y] == 'O' && !isedge[find(i)]) board[x][y] = 'X';
        }
    }
};


你可能感兴趣的:(leetcode)