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

这一题主要就是考察图的遍历方法,从外考虑O是正确思路,如果从内考察O将会超时。

Solution 1(从外考察O)

  • 遍历四条边上的O,并深度遍历与其相连的O,将这些O都转为1

  • 1代表没被包围,再将这些1都变为O

void dfs(int rows, int cols, vector<vector<char>> &board){
    if(rows < 0 || rows >= board.size() || cols < 0 || cols >= board[0].size()) return ;
     
    if(board[rows][cols] == 'O'){
        board[rows][cols] = '1';
        dfs(rows-1, cols, board);
        dfs(rows+1, cols, board);
        dfs(rows, cols-1, board);
        dfs(rows, cols+1, board);
    }
}
void solve(vector<vector<char>> &board) {
    if(board.size() <= 0) return;
    int rows = int(board.size());
    int cols = int(board[0].size());
    for (int i = 0; i < rows; ++i) {
        dfs(i, 0, board);
        dfs(i, cols-1, board);
    }
    for (int i = 0; i < cols; ++i) {
        dfs(0, i, board);
        dfs(rows-1, i, board);
    }
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            if(board[i][j] == '1')
                board[i][j] = 'O';
            else
                board[i][j] = 'X';
        }
    }
}

Solution 2(从内考察O)--维数较大时超时

#include <iostream>
#include<vector>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
int offset[4][2]={1,0,0,1,-1,0,0,-1};
class Solution {
public:
    void solve(vector<vector<char>> &arr) {
        vector<vector<int> >state(arr.size(),vector<int>(arr[0].size()));
        for(int i=0;i<arr.size();++i)
            for(int j=0;j<arr[i].size();++j)
                state[i][j]=0;
        for(int i=0;i<arr.size();++i)
            for(int j=0;j<arr[i].size();++j)
            {
                if(arr[i][j]=='X') continue;
                if(state[i][j]==0) bfs(arr,i,j,state);
            }
    }
private:
    void bfs(vector<vector<char>> &vec,int x,int y,vector<vector<int> >&visited)
    {
        if(x==0||y==0||x==vec.size()-1||y==vec[x].size()-1)
            return ;
        bool flag=true;
        visited[x][y]=1;
        queue<pair<int,int> >q;
        q.push(pair<int,int>(x,y));
        queue<pair<int,int> >pathO;
        pathO.push(pair<int,int>(x,y));
        while(!q.empty())
        {
            pair<int,int>front=q.front();
            q.pop();
            for(int i=0;i<4;++i)
            {
                int tx=front.first+offset[i][0];
                int ty=front.second+offset[i][1];
                if(tx<0||ty<0||tx>vec.size()-1||ty>vec[tx].size()-1
                  ||vec[tx][ty]=='X') continue;
                if(tx==0||ty==0||tx==vec.size()-1||ty==vec[tx].size()-1)
                {
                    flag=false;
                    break;
                }
                q.push(pair<int,int>(tx,ty));
                pathO.push(pair<int,int>(tx,ty));
                visited[tx][ty]=1;
            }
            if(!flag) return ;
        }
        if(flag)
        {
            while(!pathO.empty())
            {
                pair<int,int>pos=pathO.front();
                vec[pos.first][pos.second]='X';
                pathO.pop();
            }
        }
    }
};
int main()
{
    int n;
    cin>>n;
    vector<vector<char> >in(n,vector<char>(n));
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
        cin>>in[i][j];
    Solution res;
    res.solve(in);
    for(int i=0;i<in.size();++i)
    {
        for(int j=0;j<in[i].size();++j)
        cout<<in[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值