[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

分析

广搜。
从四个边向里面搜索,只要是遇到的’O’,都是可以从外边界走通的,都会用’.’标记
广搜一遍之后,一个一个元素遍历, 如果是’.’说明这个’O’是可以从外界走通的,保留
如果是’O’,说明这是闭塞的,从外界走不通的,替换为’X’。

代码

    /**------------------------------------
    *   日期:2015-02-06
    *   作者:SJF0115
    *   题目: 130.Surrounded Regions
    *   网址:https://oj.leetcode.com/problems/surrounded-regions/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        void solve(vector<vector<char> > &board) {
            int row = board.size();
            if(row == 0){
                return;
            }//if
            int col = board[0].size();
            // 都够不成围绕
            if(row <= 2 || col <= 2){
                return;
            }//if
            // 行
            for(int i = 0;i < col;++i){
                // 第一行
                BFS(board,row,col,0,i);
                // 最后一行
                BFS(board,row,col,row-1,i);
            }//for
            // 列
            for(int j = 0;j < row;++j){
                // 第一列
                BFS(board,row,col,j,0);
                // 最后一列
                BFS(board,row,col,j,col-1);
            }//for
            for(int i = 0;i < row;++i){
                for(int j = 0;j < col;j++){
                    // 不可以从外界走通的o
                    if(board[i][j] == 'O'){
                        board[i][j] = 'X';
                    }//if
                    // 可以从外界走通的o
                    else if(board[i][j] == '.'){
                        board[i][j] = 'O';
                    }
                }//for
            }//for
        }
    private:
        // row 行数 col 列数 x ,y 当前board位置
        void BFS(vector<vector<char>> &board,int row,int col,int x,int y){
            queue<pair<int,int> > q;
            Pass(board,row,col,x,y,q);
            while(!q.empty()){
                pair<int,int> point = q.front();
                q.pop();
                x = point.first;
                y = point.second;
                // left
                Pass(board,row,col,x,y+1,q);
                // right
                Pass(board,row,col,x,y-1,q);
                // up
                Pass(board,row,col,x-1,y,q);
                // down
                Pass(board,row,col,x+1,y,q);
            }//while
        }
        // 四边判断是否走通
        void Pass(vector<vector<char>> &board,int row,int col,int x,int y,queue<pair<int,int> > &q){
            // 边界条件以及遇到o才能走通
            if(x < 0 || x >= row || y < 0 || y >= col || board[x][y] != 'O'){
                return;
            }//if
            // 标记可从外界走通的o
            board[x][y] = '.';
            // 入队列
            q.push(make_pair(x,y));
        }
    };

    int main(){
        Solution s;
        /*vector<vector<char> > board = {
            {'X','X','X','X'},
            {'X','O','O','X'},
            {'X','X','O','X'},
            {'X','O','X','X'}
        };*/
        vector<vector<char> > board = {
            {'X','X','X'},
            {'X','O','X'},
            {'X','X','X'}
        };
        s.solve(board);
        // 输出
        for(int i = 0;i < board.size();i++){
            for(int j = 0;j < board[i].size();j++){
                cout<<board[i][j]<<" ";
            }//for
            cout<<endl;
        }//for
        return 0;
    }

运行时间

这里写图片描述

目录
相关文章
LeetCode 130. Surrounded Regions
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。
58 0
LeetCode 130. Surrounded Regions
[LeetCode] Surrounded Regions
This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low.
770 0
|
28天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
50 6
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
43 4
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
99 2
|
28天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
47 7