LeetCode之Surrounded Regions

Surrounded Regions Feb 22 2901 / 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 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

运行结果:

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);
                                       
                    }        
                }        
        }       
    }
    
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值