leetcode——130——Surrounded Regions

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

DFS(栈实现)

struct POS
{
    int x;
    int y;
    POS(int newx, int newy): x(newx), y(newy) {}
};

class Solution {
public:
    void solve(vector<vector<char>> &board) {
        if(board.empty() || board[0].empty())
            return;
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                {
                    if(i == 0 || i == m-1 || j == 0 || j == n-1)
                    {// remain 'O' on the boundry
                        dfs(board, i, j, m, n);
                    }
                }
            }
        }
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == '*')
                    board[i][j] = 'O';
            }
        }
    }
    void dfs(vector<vector<char>> &board, int i, int j, int m, int n)
    {
        stack<POS*> stk;
        POS* pos = new POS(i, j);
        stk.push(pos);
        board[i][j] = '*';
        while(!stk.empty())
        {
            POS* top = stk.top();
            if(top->x > 0 && board[top->x-1][top->y] == 'O')
            {
                POS* up = new POS(top->x-1, top->y);
                stk.push(up);
                board[up->x][up->y] = '*';
                continue;
            }
            if(top->x < m-1 && board[top->x+1][top->y] == 'O')
            {
                POS* down = new POS(top->x+1, top->y);
                stk.push(down);
                board[down->x][down->y] = '*';
                continue;
            }
            if(top->y > 0 && board[top->x][top->y-1] == 'O')
            {
                POS* left = new POS(top->x, top->y-1);
                stk.push(left);
                board[left->x][left->y] = '*';
                continue;
            }
            if(top->y < n-1 && board[top->x][top->y+1] == 'O')
            {
                POS* right = new POS(top->x, top->y+1);
                stk.push(right);
                board[right->x][right->y] = '*';
                continue;
            }
            stk.pop();
        }
    }
};

BFS(队列实现)

struct POS
{
    int x;
    int y;
    POS(int newx, int newy): x(newx), y(newy) {}
};

class Solution {
public:
    void solve(vector<vector<char>> &board) {
        if(board.empty() || board[0].empty())
            return;
        int m = board.size();
        int n = board[0].size();
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                {
                    if(i == 0 || i == m-1 || j == 0 || j == n-1)
                    {// remain 'O' on the boundry
                        bfs(board, i, j, m, n);
                    }
                }
            }
        }
        for(int i = 0; i < m; i ++)
        {
            for(int j = 0; j < n; j ++)
            {
                if(board[i][j] == 'O')
                    board[i][j] = 'X';
                else if(board[i][j] == '*')
                    board[i][j] = 'O';
            }
        }
    }
    void bfs(vector<vector<char>> &board, int i, int j, int m, int n)
    {
        queue<POS*> q;
        board[i][j] = '*';
        POS* pos = new POS(i, j);
        q.push(pos);
        while(!q.empty())
        {
            POS* front = q.front();
            q.pop();
            if(front->x > 0 && board[front->x-1][front->y] == 'O')
            {
                POS* up = new POS(front->x-1, front->y);
                q.push(up);
                board[up->x][up->y] = '*';
            }
            if(front->x < m-1 && board[front->x+1][front->y] == 'O')
            {
                POS* down = new POS(front->x+1, front->y);
                q.push(down);
                board[down->x][down->y] = '*';
            }
            if(front->y > 0 && board[front->x][front->y-1] == 'O')
            {
                POS* left = new POS(front->x, front->y-1);
                q.push(left);
                board[left->x][left->y] = '*';
            }
            if(front->y < n-1 && board[front->x][front->y+1] == 'O')
            {
                POS* right = new POS(front->x, front->y+1);
                q.push(right);
                board[right->x][right->y] = '*';
            }
        }
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值