首页 > 其他分享 >surrounded-regions

surrounded-regions

时间:2023-06-01 17:32:22浏览次数:47  
标签:int surrounded Col regions board row col Row


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,最后将设为*标志的恢复成X。



class Solution {
public:
    void solve(vector<vector<char>> &board) {
        int row = board.size();
        int col = board[0].size();
        if(row <= 2 || col <= 2)  return;
        for(int i = 0; i < row; i++){
            for(int j = 0; j < col; j++){
                if((board[i][j] == 'O' && i == 0) || (board[i][j] == 'O' && i == row - 1))
                    dfs(board, i, j);
                else if((board[i][j] == 'O' && j == 0) || (board[i][j] == 'O' && j == col - 1))
                    dfs(board, i, j);
            }
        }
        getResult(board);
    }
private:
    void dfs(vector<vector<char>> &board, int Row, int Col){
        if(board[Row][Col] == 'O'){
            board[Row][Col] = '*';
            if(Row - 1 >= 0)
                dfs(board, Row - 1, Col);
            if(Row + 1 <= board.size()-1)
                dfs(board, Row + 1, Col);
            if(Col - 1 >= 0)
                dfs(board, Row, Col - 1);
            if(Col + 1 < board[0].size())
                dfs(board, Row, Col + 1);
        }
        else
            return;
    }
    void getResult(vector<vector<char>> &board){
        for(int row = 0; row < board.size(); row++)
            for(int col = 0; col < board[0].size(); col++){
                if(board[row][col] == '*')
                    board[row][col] = 'O';
                else if(board[row][col] == 'O')
                    board[row][col] = 'X';
            }
    }
};





标签:int,surrounded,Col,regions,board,row,col,Row
From: https://blog.51cto.com/u_16147764/6396717

相关文章

  • 解决eclipse+myeclipse的Processing Dirty Regions错误
    http://www.javaeye.com/topic/192152我的Eclipse3.3.2+MyEclipse6.0.1在打开JSP文件时出现以下错误:Aninternalerroroccurredduring:"ProcessingDirtyRegions".org/eclipse/wst/sse/ui/internal/reconcile/validator/ValidationHelperAninternalerro......
  • UVA1392 DNA Regions
     https://www.luogu.com.cn/problem/UVA1392给定两个长度为n的字符串A和B,满足A和B都只由大写字母A、C、G、T组成。求一个长度最长的闭区间[L,R],满足对于i∈[L,R], 有不超过p%的i满足Ai≠Bi ......
  • 面试题百日百刷-HBase HRegionServer宕机如何处理
    锁屏面试题百日百刷,每个工作日坚持更新面试题。锁屏面试题app、小程序现已上线,官网地址:https://www.demosoftware.cn。已收录了每日更新的面试题的所有内容,还包含特色的解锁屏幕复习面试题、每日编程题目邮件推送等功能。让你在面试中先人一步!接下来的是今日的面试题: 1.HBase......
  • 130. Surrounded Regions
    ​​'X'​​ and ​​'O'​​ (the letter O),captureallregionssurroundedby ​​'X'​​.​​'O'​​sinto ​​'X'​​sinthatsurroundedregion.Fore......
  • echarts 报错 TypeError: Cannot read properties of undefined (reading 'regions')
    配置都写齐了,发现一直报错提示这个,找遍代码全局发现配置中的regions也写了,但是就是显示不出来最后发现是在geo中的map属性没有填写,填上即可;regiser等属性都要对应该值......
  • [ds 记录]P5901 [IOI2009]Regions
    这道题的难点,恐怕在复杂度分析(link首先我们可以自由选择把询问放到上面或下面。放到上面,等价于对每个点求其子树内有多少某颜色的点;放到下面,等价于对每个点求其祖先中有......