[leetcode]130. Surrounded Regions

来源:互联网 发布:巨人网络收购蓝洞 编辑:程序博客网 时间:2024/05/16 03:42

题目链接:https://leetcode.com/problems/surrounded-regions/#/description

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


  • First, check the four border of the matrix. If there is a element is
    'O', alter it and all its neighbor 'O' elements to '1'.
  • Then ,alter all the 'O' to 'X'
  • At last,alter all the '1' to 'O'

For example:

         X X X X           X X X X             X X X X         X X O X  ->       X X O X    ->       X X X X         X O X X           X 1 X X             X O X X         X O X X           X 1 X X             X O X X

class Solution {public:    void solve(vector<vector<char>>& board) {        int i,j;        int row=board.size();        if(!row)            return;        int col=board[0].size();        for(i=0;i<row;i++)        {            check(board,i,0,row,col);            if(col>1)                check(board,i,col-1,row,col);        }        for(j=1;j+1<col;j++)        {            check(board,0,j,row,col);            if(row>1)                check(board,row-1,j,row,col);        }        for(i=0;i<row;i++)        {            for(j=0;j<col;j++)            {                if(board[i][j]=='O')                    board[i][j]='X';            }        }        for(i=0;i<row;i++)        {            for(j=0;j<col;j++)            {                if(board[i][j]=='1')                    board[i][j]='O';            }        }    }    void check(vector<vector<char>>& vec,int i,int j,int row,int col)    {        if(vec[i][j]=='O')        {            vec[i][j]='1';            if(i>1)                check(vec,i-1,j,row,col);            if(j>1)                check(vec,i,j-1,row,col);            if(i+1<row)                check(vec,i+1,j,row,col);            if(j+1<col)                check(vec,i,j+1,row,col);        }    }};


原创粉丝点击