• 130. Surrounded Regions


    Given a 2D board containing 'X' and 'O' (the letter 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

    public void Solve(char[,] board) {
            if(board.GetLength(0)==0 || board.GetLength(0)==0 ) return;
            int rowLength = board.GetLength(0);
            int colLength = board.GetLength(1);
     
            for(int i = 0;i<rowLength;i++)
            {
                for(int j = 0;j<colLength;j++)
                {
                    if((i==0||j==0||i == rowLength-1 || j == colLength -1)&&(board[i,j]=='O'))
                    {
                        DFS(board,i,j);
                    }
                }
            }
            for(int i = 0;i<rowLength;i++)
            {
                for(int j = 0;j<colLength;j++)
                {
                   if(board[i,j]=='T')  board[i,j]='O';
                   else if(board[i,j]=='O')  board[i,j]='X';
                }
            }
            return;
            
        }
        
        private void DFS(char[,] board, int row, int col)
        {
            board[row,col] = 'T';
            if(row>0 && board[row-1,col] == 'O') DFS(board,row-1,col);
            if(row<(board.GetLength(0)-1) && board[row+1,col] == 'O') DFS(board,row+1,col);
            if(col>1 && board[row,col-1] == 'O') DFS(board,row,col-1);
            if(col<(board.GetLength(1)-1) && board[row,col+1] == 'O') DFS(board,row,col+1);
        }
  • 相关阅读:
    oracle中文显示为问号
    oracle 11g 安装报错(agent_nmhs)
    yum源配置
    ora-00020
    mysql停止正在执行的SQL语句
    linux root用户被锁定
    MySQL8.0 根据ibd文件恢复表结构
    mysql 8.x 开启远程访问和修改root密码、
    个人博客迁移到github
    postman断言方式
  • 原文地址:https://www.cnblogs.com/renyualbert/p/5898535.html
Copyright © 2020-2023  润新知