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, the board should be:

X X X X
X X X X
X X X X
X O X X

感染列岛 哈哈

挺有意思一道题,讲解 和code http://www.cnblogs.com/huntfor/p/3898068.html

题解

// XXXXX -->  X X X X X --> XXXX
// XOXOX    X# X O X   XOXX
// XOXOX    X# X O X   XOXX
// XOXXX    X# X X X   XOXX

用queue来存储,这样后面加进来的传染源就不会干扰之前的传染源传染

public class Solution {
    public void solve(char[][] board) {
        if(board==null||board.length<=1||board[0].length<=1 ) return;
        for(int i=0;i<board.length;i++){ // fist col
            helper(board, i, 0);
            helper(board, i, board[0].length-1); // last col
        }
        for(int j=0;j<board[0].length;j++){ //first row
            helper(board, 0, j);
             helper(board, board.length-1, j); // last row
        }
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]!=‘#‘){
                    board[i][j] = ‘X‘;
                }else
                    board[i][j] = ‘O‘;
            }
        }
    }
    private void helper(char[][] b, int i, int j){
        if(b[i][j]!=‘O‘) return;
        b[i][j]=‘#‘;
        int code = b[0].length*i+j;

        LinkedList<Integer> queue = new LinkedList<Integer>(); // 为了从外围逐渐向中心渲染
        queue.add(code);
        while(!queue.isEmpty()){
            int c = queue.poll();
            int row = c/b[0].length;
            int col = c%b[0].length;
            //up
            if(row>0 && b[row-1][col]==‘O‘){
                b[row-1][col]=‘#‘;
                queue.add(b[0].length*(row-1)+col);
            }
            //down
            if(row<b.length-1 && b[row+1][col]==‘O‘){
                b[row+1][col]=‘#‘;
                queue.add(b[0].length*(row+1)+col);
            }
            //left
            if(col>0 && b[row][col-1]==‘O‘){
                b[row][col-1]=‘#‘;
                queue.add(b[0].length*(row)+col-1);
            }
            //right
            if(col< b[0].length-1 && b[row][col+1]==‘O‘){
                b[row][col+1]=‘#‘;
                queue.add(b[0].length*(row)+col+1);
            }
        }
    }
}
时间: 2024-10-09 08:44:19

Surrounded Regions 感染列岛的相关文章

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

[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][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