• [LeetCode] 130. 被围绕的区域


    题目链接 : https://leetcode-cn.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连通点找到, 把这些变成B,然后遍历整个boardO变成X, 把B变成O

    如下图所示

    所以这样就有2种方法

    思路一: DFS

    思路二: BFS

    还有一种就是, 把边界O并且与它连通这些点分在一起

    思路三 : 并查集

    代码:

    思路一:

    class Solution:
        def solve(self, board: List[List[str]]) -> None:
            """
            Do not return anything, modify board in-place instead.
            """
            if not board or not board[0]:
                return
            row = len(board)
            col = len(board[0])
    
            def dfs(i, j):
                board[i][j] = "B"
                for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    tmp_i = i + x
                    tmp_j = j + y
                    if 1 <= tmp_i < row and 1 <= tmp_j < col and board[tmp_i][tmp_j] == "O":
                        dfs(tmp_i, tmp_j)
    
            for j in range(col):
                # 第一行
                if board[0][j] == "O":
                    dfs(0, j)
                # 最后一行
                if board[row - 1][j] == "O":
                    dfs(row - 1, j)
    
            for i in range(row):
                # 第一列
                if board[i][0] == "O":
                    dfs(i, 0)
                # 最后一列
                if board[i][col-1] == "O":
                    dfs(i, col - 1)
    
            for i in range(row):
                for j in range(col):
                    # O 变成 X
                    if board[i][j] == "O":
                        board[i][j] = "X"
                    # B 变成 O
                    if board[i][j] == "B":
                        board[i][j] = "O"
    

    java

    class Solution {
        int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    
        public void solve(char[][] board) {
            if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) return;
            int row = board.length;
            int col = board[0].length;
            for (int j = 0; j < col; j++) {
                // 第一行
                if (board[0][j] == 'O') dfs(0, j, board, row, col);
                // 最后一行
                if (board[row - 1][j] == 'O') dfs(row - 1, j, board, row, col);
            }
    
            for (int i = 0; i < row; i++) {
                // 第一列
                if (board[i][0] == 'O') dfs(i, 0, board, row, col);
                // 最后一列
                if (board[i][col - 1] == 'O') dfs(i, col - 1, board, row, col);
            }
    
            // 转变
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (board[i][j] == 'O') board[i][j] = 'X';
                    if (board[i][j] == 'B') board[i][j] = 'O';
                }
            }
    
        }
    
        private void dfs(int i, int j, char[][] board, int row, int col) {
            board[i][j] = 'B';
            for (int[] dir : dirs) {
                int tmp_i = dir[0] + i;
                int tmp_j = dir[1] + j;
                if (tmp_i < 0 || tmp_i >= row || tmp_j < 0 || tmp_j >= col || board[tmp_i][tmp_j] != 'O') continue;
                dfs(tmp_i, tmp_j, board, row, col);
            }
        }
    }
    

    思路二:

    class Solution:
        def solve(self, board: List[List[str]]) -> None:
            """
            Do not return anything, modify board in-place instead.
            """
            if not board or not board[0]:
                return
            row = len(board)
            col = len(board[0])
    
            def bfs(i, j):
                from collections import deque
                queue = deque()
                queue.appendleft((i, j))
                while queue:
                    i, j = queue.pop()
                    if 0 <= i < row and 0 <= j < col and board[i][j] == "O":
                        board[i][j] = "B"
                        for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                            queue.appendleft((i + x, j + y))
    
            for j in range(col):
                # 第一行
                if board[0][j] == "O":
                    bfs(0, j)
                # 最后一行
                if board[row - 1][j] == "O":
                    bfs(row - 1, j)
    
            for i in range(row):
    
                if board[i][0] == "O":
                    bfs(i, 0)
                if board[i][col - 1] == "O":
                    bfs(i, col - 1)
    
            for i in range(row):
                for j in range(col):
                    if board[i][j] == "O":
                        board[i][j] = "X"
                    if board[i][j] == "B":
                        board[i][j] = "O"
    

    java

    class Solution {
        int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
        private static class Point {
            int x, y;
    
            Point(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
    
        public void solve(char[][] board) {
            if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) return;
            int row = board.length;
            int col = board[0].length;
            for (int j = 0; j < col; j++) {
                // 第一行
                if (board[0][j] == 'O') bfs(0, j, board, row, col);
                // 最后一行
                if (board[row - 1][j] == 'O') bfs(row - 1, j, board, row, col);
            }
    
            for (int i = 0; i < row; i++) {
                // 第一列
                if (board[i][0] == 'O') bfs(i, 0, board, row, col);
                // 最后一列
                if (board[i][col - 1] == 'O') bfs(i, col - 1, board, row, col);
            }
    
            // 转变
            for (int i = 0; i < row; i++) {
                for (int j = 0; j < col; j++) {
                    if (board[i][j] == 'O') board[i][j] = 'X';
                    if (board[i][j] == 'B') board[i][j] = 'O';
                }
            }
    
        }
    
        private void bfs(int i, int j, char[][] board, int row, int col) {
            Deque<Point> queue = new LinkedList<>();
            queue.offer(new Point(i, j));
            while (!queue.isEmpty()) {
                Point tmp = queue.poll();
                if (tmp.x >= 0 && tmp.x < row && tmp.y >= 0 && tmp.y < col && board[tmp.x][tmp.y] == 'O') {
                    board[tmp.x][tmp.y] = 'B';
                    for (int[] dir : dirs) queue.offer(new Point(tmp.x + dir[0], tmp.y + dir[1]));
                }
            }
        }
    }
    

    思路三:

    class Solution:
        def solve(self, board: List[List[str]]) -> None:
            """
            Do not return anything, modify board in-place instead.
            """
            f = {}
            def find(x):
                f.setdefault(x, x)
                if f[x] != x:
                    f[x] = find(f[x])
                return f[x]
            def union(x, y):
                f[find(y)] = find(x)
    
                
                
            if not board or not board[0]:
                return
            row = len(board)
            col = len(board[0])
            dummy = row * col
            for i in range(row):
                for j in range(col):
                    if board[i][j] == "O":
                        if i == 0 or i == row - 1 or j == 0 or j == col - 1:
                            union(i * col + j, dummy)
                        else:
                            for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                                if board[i + x][j + y] == "O":
                                    union(i * col + j, (i + x) * col + (j + y))
            for i in range(row):
                for j in range(col):
                    if find(dummy) == find(i * col + j):
                        board[i][j] = "O"
                    else:
                        board[i][j] = "X"
    
  • 相关阅读:
    自动释放池
    图片裁剪成圆形(无边框)
    根据数字对应星期几
    IOS 周几转化数字
    计算两个日期之间的天数
    java——IO流整理(一)
    java——File类的用法整理
    java——用递归和IO流来实现文件的复制
    java——java集合详解
    java——对象的克隆
  • 原文地址:https://www.cnblogs.com/powercai/p/11185272.html
Copyright © 2020-2023  润新知