leetcode:Union Find:Surrounded Regions(130)

原创 2016年08月31日 15:16:21
class Solution {  
public:  
    void process(int i,int j,vector<vector<char> >& board)  
    {  
        int m=board.size();  
        int n=board[0].size();  

        typedef pair<int,int> point;  
        queue<point> Q;  
        Q.push(point(i,j));  
        board[i][j]='E';  
        while(!Q.empty())  
        {  
            point tmp=Q.front();  
            Q.pop();  

            int x=tmp.first,y=tmp.second;  

            //extending  
            if (x!=0&&board[x-1][y]=='O')  
            {  
                Q.push(point(x-1,y));  
                board[x-1][y]='E';   //extended;  
            }  
            if (x!=m-1&&board[x+1][y]=='O')  
            {  
                Q.push(point(x+1,y));  
                board[x+1][y]='E';   //extended;  
            }  
            if (y!=0&&board[x][y-1]=='O')  
            {  
                Q.push(point(x,y-1));  
                board[x][y-1]='E';   //extended;  
            }  
            if (y!=n-1&&board[x][y+1]=='O')  
            {  
                Q.push(point(x,y+1));  
                board[x][y+1]='E';   //extended;  
            }  
        }  
    }  
    void solve(vector<vector<char> >& board)  
    {  
        int m=board.size();  
        if (m==0)  
            return;  
        int n=board[0].size();  
        int i,j;  
        for(i=0;i<m;i++)  
        {  
            if (board[i][0]=='O')  
                process(i,0,board);  
            if (board[i][n-1]=='O')  
                process(i,n-1,board);  
        }  
        for(j=0;j<n;j++)  
        {  
            if (board[0][j]=='O')  
                process(0,j,board);  
            if (board[m-1][j]=='O')  
                process(m-1,j,board);  
        }  
        for(i=0;i<m;i++)  
            for(j=0;j<n;j++)  
            {  
                if(board[i][j]=='O')  
                    board[i][j]='X';  
                else if (board[i][j]=='E')  
                    board[i][j]='O';  
            }  
    }     
};  
版权声明:本文为博主原创文章,未经博主允许不得转载。

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

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. ...

LeetCode 130. Surrounded Regions (并查集)

LeetCode 130. Surrounded Regions (并查集)LeetCode 130 Surrounded Regions 并查集 问题描述 解题思路 参考代码 Tags: - B...

[Leetcode] 130. Surrounded Regions 解题报告

题目: Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by...

(LeetCode 130)Surrounded Regions(并查集)

Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captu...

leetcode——130——Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captur...

Leetcode 130 Surrounded Regions

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A r...

LeetCode 130. Surrounded Regions

逆向思维,从边缘的O出发(上下左右四排的O),进行DFS, 将这些'O'可达的所有点'O'都标记为'Z'(包括它们自身也标记为O), 这些'Z'最终都可以保持为'O'不变,而其他点应该都变成'X'. ...

利用BFS和DFS解决 LeetCode 130: Surrounded Regions

问题来源此题来源于LEETCODE,具体问题详见下面的链接 https://leetcode.com/problems/surrounded-regions/description/问题简述给定一个...
  • zjuPeco
  • zjuPeco
  • 2017年07月30日 12:23
  • 153

[leetcode] 130. Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captur...

【LeetCode-面试算法经典-Java实现】【130-Surrounded Regions(环绕区域)】

【130-Surrounded Regions(环绕区域)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a 2D board containing 'X' ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode:Union Find:Surrounded Regions(130)
举报原因:
原因补充:

(最多只允许输入30个字)