lintcode:被围绕的区域

被围绕的区域

给一个二维的矩阵,包含 ‘X‘ 和 ‘O‘, 找到所有被 ‘X‘ 围绕的区域,并用 ‘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
X O X X

解题参考岛屿的个数  然而我还是写不出来programcreek 有下面的一段话:

This problem is similar to Number of Islands. In this problem, only the cells on the
boarders can not be surrounded. So we can first merge those O’s on the boarders like
in Number of Islands and replace O’s with ’#’, and then scan the board and replace all
O’s left (if any)

先将边界中联通的O换成*,这里利用深度优先

最后遍历数组,O->X  *->O,每个位置值遍历一次,不会出现混乱

public class Solution {
    /**
     * @param board a 2D board containing ‘X‘ and ‘O‘
     * @return void
     */
    public void surroundedRegions(char[][] board) {
        // Write your code here
        if(board == null || board.length == 0|| board[0].length == 0)
            return ;
        int row = board.length;
        int col = board[0].length;
        for(int i =0;i< row;i++){
            if(board[i][0] == ‘O‘)
                merge(board,i,0);
            if(board[i][col-1] == ‘O‘)
                merge(board,i,col-1);
        }
        for(int j =0;j< col;j++){
            if(board[0][j] == ‘O‘)
                merge(board,0,j);
            if(board[row-1][j] == ‘O‘)
                merge(board,row-1,j);
        }
        // O->X  *->O
        for(int i=0;i<row;i++){
            for(int j=0; j<col; j++){
                if(board[i][j] == ‘O‘){
                    board[i][j] = ‘X‘;
                }else if(board[i][j] == ‘*‘){
                    board[i][j] = ‘O‘;
                }
            }
        }
    }
    // 将边界联通的O 换成 *
    public void merge(char[][] board,int row,int col){
        if(row<0 || col<0 || row>= board.length || col>= board[0].length)
            return;
        if(board[row][col] != ‘O‘)
            return;
        board[row][col] = ‘*‘;
        merge(board,row,col+1);
        merge(board,row+1,col);
        merge(board,row-1,col);
        merge(board,row,col-1);
    }
}
This solution causes java.lang.StackOverflowError, because for a large board, too many method calls are pushed to the stack and causes the overflow.

programcreek 中又给了一个广度优先的算法

比较复杂,先把程序复杂过来

public class Solution {
    // use a queue to do BFS
    private Queue<Integer> queue = new LinkedList<Integer>();

    public void solve(char[][] board) {
        if (board == null || board.length == 0)
            return;

        int m = board.length;
        int n = board[0].length;

        // merge O‘s on left & right boarder
        for (int i = 0; i < m; i++) {
            if (board[i][0] == ‘O‘) {
                bfs(board, i, 0);
            }

            if (board[i][n - 1] == ‘O‘) {
                bfs(board, i, n - 1);
            }
        }

        // merge O‘s on top & bottom boarder
        for (int j = 0; j < n; j++) {
            if (board[0][j] == ‘O‘) {
                bfs(board, 0, j);
            }

            if (board[m - 1][j] == ‘O‘) {
                bfs(board, m - 1, j);
            }
        }

        // process the board
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == ‘O‘) {
                    board[i][j] = ‘X‘;
                } else if (board[i][j] == ‘#‘) {
                    board[i][j] = ‘O‘;
                }
            }
        }
    }

    private void bfs(char[][] board, int i, int j) {
        int n = board[0].length;

        // fill current first and then its neighbors
        fillCell(board, i, j);

        while (!queue.isEmpty()) {
            int cur = queue.poll();
            int x = cur / n;
            int y = cur % n;

            fillCell(board, x - 1, y);
            fillCell(board, x + 1, y);
            fillCell(board, x, y - 1);
            fillCell(board, x, y + 1);
        }
    }

    private void fillCell(char[][] board, int i, int j) {
        int m = board.length;
        int n = board[0].length;
        if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] != ‘O‘)
            return;

        // add current cell is queue & then process its neighbors in bfs
        queue.offer(i * n + j);
        board[i][j] = ‘#‘;
    }
}
 
时间: 2024-10-12 03:27:53

lintcode:被围绕的区域的相关文章

lintcode - 被围绕的区域

1 class Solution { 2 public: 3 /* 4 * @param board: board a 2D board containing 'X' and 'O' 5 * @return: nothing 6 */ 7 bool flag[220][220]; 8 vector<pair<int, int>> v; 9 const int dir[4][2] = {{0,1}, {1,0}, {0, -1}, {-1, 0}}; 10 int isChange

算法-被围绕的区域

做面试题,做面试题! 题意: 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 '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 X O X X 这个题才开始看上去根本没有思路,根本就不知道从哪里下手才好.借鉴了其他人的思想,我才知道是怎么做的. 1.解题思路 这个题最重要的一个特点就是,我们需要将被围绕起来的O换成X

被围绕的区域 &#183; Surrounded Regions

[抄题]: 给一个二维的矩阵,包含 'X' 和 'O', 找到所有被 'X' 围绕的区域,并用 '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 X O X X [暴力解法]: 时间分析: 空间分析: [思维问题]: 找四周都是平原的盆地感觉不好找:可以尝试逆向思维,找开了口和外界有联系的盆地 "注水"的过程通过图形化抽象后再用数学表达出来是bf

【1】【leetcode-130】 被围绕的区域

(DFS思路对,写复杂了) 给定一个二维的矩阵,包含 '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' 最终都会被填充为

130被围绕的区域

题目:给定一个二维的矩阵,包含 'X' 和 'O'(字母 O).找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 来源:https://leetcode-cn.com/problems/surrounded-regions/ 法一:自己的代码 思路:先绕外围走一圈,将所有与外围相连的岛屿都标记为True,然后把bool数组中位置为False的置为‘X’,为了节省空间可以直接将外围的‘O’,改为‘A’,然后再替换. from typing import List c

[LintCode] 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.Example X X X XX O O XX X O XX O X X After capture all regions surrounded by 'X', the boar

leetccode-130-被围绕的区域

题目描述: 方法一:dfs class Solution: def solve(self, board: List[List[str]]) -> None: """ Do not return anything, modify board in-place instead. """ if not board or len(board)<=2 or len(board[0])<=2: return row = len(board)

DFS+并查集思想求被围绕的区域

class Solution { private int[][] dir= {{0,-1},{-1,0},{0,1},{1,0}}; private boolean[][] used; public boolean isMove(char[][] board,int x,int y) { if(x>=0&&x<board.length&&y>=0&&y<board[0].length) return true; return fals

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  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 fu