LeetCode之Surrounded Regions

来源:互联网 发布:手机弹古筝软件 编辑:程序博客网 时间:2024/04/29 02:25

Surrounded RegionsFeb 222901 / 10932

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

运行结果:

Run Status: Accepted!
Program Runtime: 12 milli secs


class Solution {

private:
       void change_char(vector<vector<char> > &board, int row, int col, vector<vector<int> > &flag)
       {
                int i, j, x_num=0;
                int dir[4][2];
                int total_row = board.size();
                int total_col = board[0].size();
                

                flag[row][col] = 1;


  //设定左,上,右,下四个方向              
                dir[0][0] = 0;
                dir[0][1] = -1; 
                dir[1][0] = -1;
                dir[1][1] = 0;
                dir[2][0] = 0;
                dir[2][1] = 1;
                dir[3][0] = 1;
                dir[3][1] = 0;
       
                
                for(i=0;i<4;i++)
                {
                 

                    int cur_row = row+dir[i][0], cur_col = col+dir[i][1];

//判断是否越界

                    if(cur_row < 0 || cur_col < 0 || cur_row >= total_row || cur_col >= total_col)
                        continue;
                  
                    if(board[cur_row][cur_col] == 'X')
                    {
                        x_num++;                                            
                    }           
                    else if(!flag[cur_row][cur_col] && board[cur_row][cur_col] == 'O')
                    {
                        board[row][col] = 'X';
                        change_char(board, cur_row, cur_col, flag);   
                        
                        if(board[cur_row][cur_col] == 'X') 
                            x_num++; 
                            
                        else{ 
                              board[row][col] = 'O';
                              return;
                        }
                    }     
                }
                if(x_num == 4) board[row][col] = 'X';               
       }


public:
    void solve(vector<vector<char> > &board) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if(board.empty()) return;
        
        vector<vector<char> >::size_type row = board.size();
        vector<vector<char> >::size_type col = board[0].size();
        

       //标记数组,用来标记board数组中相应位置的值是否被遍历过,0表示未遍历,1表示遍历

//首先把边界都设为1,从而边界上的o就不会被修改 

        vector<int>flag_col(col);
       
        vector<vector<int> > flag;
        for(int i=0; i<row; i++)
        {
            flag.push_back(flag_col);        
        }
        
        for(int i = 0; i<col; i++)
        {
                flag[0][i] = 1;
                flag[row-1][i] = 1;
        }
        for(int i = 0; i<row; i++)
        {
                flag[i][0] = 1;
                flag[i][col-1] = 1;        
        }
        
        
        for(int i=0; i<row; i++)
        {
                for(int j=0; j<col; j++)
                {
                    if(board[i][j] == 'O')
                    {
                        if(0==i || i == (row-1) || 0==j || j ==(col-1))
                        {
                            continue;
                        }
                        change_char(board, i, j, flag);
                                       
                    }        
                }        
        }       
    }
    
};