lintcode-medium-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 X
X O O X
X X O X
X O X X

After capture all regions surrounded by ‘X‘, the board should be:

X X X X
X X X X
X X X X
X O X X
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 <= 1 || board[0] == null || board[0].length <= 1)
            return;

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

        for(int i = 0; i < m; i++)
            if(board[i][0] == ‘O‘)
                flood(board, i, 0);

        for(int i = 0; i < m; i++)
            if(board[i][n - 1] == ‘O‘)
                flood(board, i, n - 1);

        for(int i = 0; i < n; i++)
            if(board[0][i] == ‘O‘)
                flood(board, 0, i);

        for(int i = 0; i < n; i++)
            if(board[m - 1][i] == ‘O‘)
                flood(board, m - 1, i);

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

        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(board[i][j] == ‘%‘)
                    board[i][j] = ‘O‘;
            }
        }

        return;
    }

    public void flood(char[][] board, int i, int j){

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

        if(i >= m || i < 0 || j >= n || j < 0)
            return;

        if(board[i][j] == ‘O‘){
            board[i][j] = ‘%‘;
            flood(board, i + 1, j);
            flood(board, i - 1, j);
            flood(board, i, j + 1);
            flood(board, i, j - 1);
        }

        return;
    }
}
时间: 2024-11-04 09:02:43

lintcode-medium-Surrounded Regions的相关文章

[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

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

LeetCode: Surrounded Regions 解题报告

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 XX O O XX X O XX O X XAfter running your function, the

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

[LeetCode][JavaScript]Surrounded Regions

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 function

【leetcode】Surrounded Regions

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 function

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

Surrounded Regions LeetCode :My Solution

Surrounded Regions Total Accepted: 14948 Total Submissions: 105121My Submissions 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 exampl

LeetCode: Surrounded Regions [130]

[题目] 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 sh

验证LeetCode Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =