130. 被围绕的区域

题目

(https://leetcode.com/problems/surrounded-regions/)
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O)。

找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。

示例:

X X X X
X O O X
X X O X
X O X X
运行你的函数后,矩阵变为:

X X X X
X X X X
X X X X
X O X X
解释:

被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。

分析

换一个思路。从边界开始。只要与边界O有连接的,最后肯定是没有被X包围的。
从四边开始遍历。被O连接的地方,改成Z,然后把这个地方入栈遍历。

四边遍历完后。把内部的O都变成X。因为内部的O都是被X包围的。
然后再吧Z改成O

代码

class Solution {
    public void solve(char[][] board) {
        if (board.length>2 && board[0].length>2){
                Stack stack = new Stack<>();
                //遍历四边
                for (int i = 1; i < board.length-1; i++) {
                    if (board[i][0] == 'O'){
                        if (board[i][1] == 'O'){
                            board[i][1] = 'Z';
                            int [] temp ={i,1};
                            stack.push(temp);
                        }
                    }
                    while (!stack.empty()){
                        int[] pop = stack.pop();
                        setX(board,pop,stack);
                    }
                }

                for (int i = 1; i < board.length-1; i++) {
                    if (board[i][board[0].length-1] == 'O'){
                        if (board[i][board[0].length-2] == 'O'){
                            board[i][board[0].length-2] = 'Z';
                            int [] temp ={i,board[0].length-2};
                            stack.push(temp);
                        }
                    }
                    while (!stack.empty()){
                        int[] pop = stack.pop();
                        setX(board,pop,stack);
                    }
                }

                for (int i = 1; i < board[0].length-1; i++) {
                    if (board[0][i] == 'O'){
                        if (board[1][i] == 'O'){
                            board[1][i] = 'Z';
                            int [] temp ={1,i};
                            stack.push(temp);
                        }
                    }
                    while (!stack.empty()){
                        int[] pop = stack.pop();
                        setX(board,pop,stack);
                    }
                }

                for (int i = 1; i < board[0].length-1; i++) {
                    if (board[board.length-1][i] == 'O'){
                        if (board[board.length-2][i] == 'O'){
                            board[board.length-2][i] = 'Z';
                            int [] temp ={board.length-2,i};
                            stack.push(temp);
                        }
                    }
                    while (!stack.empty()){
                        int[] pop = stack.pop();
                        setX(board,pop,stack);
                    }
                }
            }

            for (int i = 1; i < board.length-1; i++) {
                for (int j = 1; j < board[i].length-1; j++) {
                    if (board[i][j] == 'O'){
                        board[i][j] = 'X';
                    }
                    if (board[i][j] == 'Z'){
                        board[i][j] = 'O';
                    }

                }
            }
    }
    
    
    private  void setX(char[][] board,int[] pop, Stack stack){
             //up
            if (pop[1]-1>0 && board[pop[0]][pop[1]-1] == 'O'){
                board[pop[0]][pop[1]-1] = 'Z';
                int [] temp ={pop[0],pop[1]-1};
                stack.push(temp);
            }
            //down
            if (pop[1]+10 && board[pop[0]-1][pop[1]] == 'O'){
                board[pop[0]-1][pop[1]] = 'Z';
                int [] temp ={pop[0]-1,pop[1]};
                stack.push(temp);
            }
        }
}

结果

image.png

你可能感兴趣的:(130. 被围绕的区域)