LeetCode: Surrounded Regions [130]

来源:互联网 发布:股票相关书籍推荐知乎 编辑:程序博客网 时间:2024/04/29 12:02

【题目】

Given a 2D board containing 'X' and '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 XX O O XX X O XX O X X

After running your function, the board should be:

X X X XX X X XX X X XX O X X


【题意】

给定一个二维矩阵,由'X'和'O'填充,本题要求把那些被'X'包围的'O'替换为'X'。注意这里的包围是指四周完全包围。如果某片'O'区域触及了矩阵的边界,则这片区域就不算被'X'包围。


【思路】

    我们只需要先把触及到边界的'O'区域全部替换成另一个字母, 比如'M'。矩阵中剩下的'O'区域就肯定是被包围的了。
    然后,我们扫描矩阵把'O'替换成'X', 把'M'替换回'O'即可
    
    区域的遍历,使用DFS或BFS
    DFS要使用递归,当矩阵很大的时候容易超时
    所以本题使用BFS

    


【代码】

class Solution {public:    void O2M(vector<vector<char> >&board, int i, int j){        //从board[i][j]开始,遍历'O'区域,把'O'替换成'M'        int rows=board.size();        int cols=board[0].size();        queue<int> qe;        qe.push(i*cols+j);        board[i][j]='M';        while(!qe.empty()){            int pos = qe.front(); qe.pop();            i=pos/cols;            j=pos%cols;            //判断上方            if(i-1>=0 && board[i-1][j]=='O'){                board[i-1][j]='M';  //注意在将相邻元素填到队列中时,需要将它标记为以访问,否则以后在确定其他节点的'O'相邻位置时,很有可能又把这个节点加入到队列中。造成死循环。                qe.push((i-1)*cols + j);            }            //判断下方            if(i+1<rows && board[i+1][j]=='O'){                board[i+1][j]='M';                qe.push((i+1)*cols + j);            }            //判断左方            if(j-1>=0 && board[i][j-1]=='O'){                board[i][j-1]='M';                qe.push(i*cols + j-1);            }            //判断右方            if(j+1<cols && board[i][j+1]=='O'){                board[i][j+1]='M';                qe.push(i*cols + j+1);            }        }    }    void solve(vector<vector<char>> &board) {        int rows=board.size();        if(rows==0)return;        int cols=board[0].size();        if(cols==0)return;                //把临边的'O'区域替换成'M'        //上边        for(int j=0; j<cols; j++){            if(board[0][j]=='O')O2M(board, 0, j);        }        //下边        for(int j=0; j<cols; j++){            if(board[rows-1][j]=='O')O2M(board, rows-1, j);        }        //左边        for(int i=0; i<rows; i++){            if(board[i][0]=='O')O2M(board, i, 0);        }        //右边        for(int i=0; i<rows; i++){            if(board[i][cols-1]=='O')O2M(board, i, cols-1);        }                //扫描矩阵,把O替换成X, 把M替换成O        for(int i=0; i<rows; i++){            for(int j=0; j<cols; j++){                if(board[i][j]=='O')board[i][j]='X';                else if(board[i][j]=='M')board[i][j]='O';            }        }            }};


0 0
原创粉丝点击