[leedcode 130] 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
public class Solution {
    public void solve(char[][] board) {
         /* 从四周BFS!!
            题意说明:输入一个二维矩阵,把所有被X包围的O都变成X,但如果某个O在边(角)上,则所有与这个O相连的O都不能变成X。
            解题思路:从边上(四个边)的O开始便利,所有与边上O相连的O都不能变成X(可暂时变为Y),之后再重新遍历一遍矩阵把所有O变成X,
            所有Y变成O。
            需要注意的是,本题使用DFS也会出现栈溢出,只能使用BFS。
            使用两个List保存为‘O‘的坐标,每次从数组中取出一对坐标(并标记为Y,表示跟外界能够相连的O),然后验证其上下左右有没有‘O‘,
            如果有add到数组。 另一种方案是构造一个队列,队列保存O的节点,从队列中取出一个节点,把该节点变为Y,
            如果该节点的四周有还未变为Y的O,则这些节点放入队列。*/
        if(board==null||board.length<2) return;
        if(board[0].length<2) return;
        ArrayList<Integer> xindex=new ArrayList<Integer>();
        ArrayList<Integer> yindex=new ArrayList<Integer>();
        int row=board.length;
        int col=board[0].length;
        for(int i=0;i<board.length;i++){
            if(board[i][0]==‘O‘){
                xindex.add(i);
                yindex.add(0);
            }
            if(board[i][col-1]==‘O‘){
                xindex.add(i);
                yindex.add(col-1);
            }

        }
        for(int j=0;j<col;j++){
            if(board[0][j]==‘O‘){
                xindex.add(0);
                yindex.add(j);
            }
            if(board[row-1][j]==‘O‘){
                xindex.add(row-1);
                yindex.add(j);
            }
        }
        int k=0;
        while(k<xindex.size()){
            int i=xindex.get(k);
            int j=yindex.get(k);
            board[i][j]=‘Y‘;
            if(i>0&&board[i-1][j]==‘O‘){//上
                xindex.add(i-1);
                yindex.add(j);
            }
            if(i<row-1&&board[i+1][j]==‘O‘){//下
                xindex.add(i+1);
                yindex.add(j);
            }
            if(j>0&&board[i][j-1]==‘O‘){//左
                xindex.add(i);
                yindex.add(j-1);
            }
            if(j<col-1&&board[i][j+1]==‘O‘){//右
                xindex.add(i);
                yindex.add(j+1);
            }
            k++;
        }
        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]==‘Y‘)
                    board[i][j]=‘O‘;
            }
        }
       /* if(board==null||board.length<=2||board[0].length<=2)return ;
        for(int i=0;i<board.length;i++){
            bfs(board,i,0);
            bfs(board,i,board[0].length-1);
        }
        for(int j=0;j<board[0].length;j++){
            bfs(board,0,j);
            bfs(board,board.length-1,j);
        }
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]==‘O‘) board[i][j]=‘X‘;
                else if(board[i][j]==‘Y‘) board[i][j]=‘O‘;
            }
        }
    }
    public void bfs(char[][]board,int row,int col){
        if(!isSurrod(board,row,col)) return;
        board[row][col]=‘Y‘;
        bfs(board,row-1,col);//上
        bfs(board,row+1,col);//下
        bfs(board,row,col-1);//左
        bfs(board,row,col+1);//右
    }
    public boolean isSurrod(char[][]board,int row,int col){
        return !(row<0||row>=board.length||col<0||col>=board[0].length||board[row][col]!=‘O‘);

        /*if(board==null||board.length==0) {
            return;
        }
        Queue queue = new LinkedList<Pair>();
    //对所有在边上的O节点进行BFS
        for(int i=0;i<board.length;i++) {
            for(int j=0;j<board[0].length;j++) {
                if(i==0||i==(board.length-1)||j==0||j==(board[0].length-1)) {
                    if(board[i][j]==‘O‘) {
                        Pair position = new Pair(i,j);
                        queue.add(position);
                    }
                }
            }
        }
        int x1,y1;
        //BFS
        while(!queue.isEmpty()) {
            Pair position = (Pair)queue.poll();
            x1 = position.x;
            y1 = position.y;  

        //四个方向查找,找到为‘O‘的节点放入队列中。
            //left
            board[x1][y1] = ‘Y‘;
            int index = x1 - 1;
            if(index>=0&&board[index][y1]==‘O‘) {
                queue.add(new Pair(index,y1));
            }
            //right
            index = x1 + 1;
            if(index<board.length&&board[index][y1]==‘O‘) {
                queue.add(new Pair(index,y1));
            }
            //up
            index = y1 + 1 ;
            if(index<board[0].length&&board[x1][index]==‘O‘) {
                queue.add(new Pair(x1,index));
            }
            //down
            index = y1 - 1;
            if(index>=0&&board[x1][index]==‘O‘) {
                queue.add(new Pair(x1,index));
            }
        }  

        for(int i=0;i<board.length;i++) {
            for(int j=0;j<board[0].length;j++) {
                if(board[i][j]==‘Y‘) {
                    board[i][j] = ‘O‘;
                } else {
                    if(board[i][j]==‘O‘) {
                        board[i][j] = ‘X‘;
                    }
                }
            }
        }
        return;  */

    }
}
class Pair {
    int x;
    int y;
    Pair(int x,int y) {
        this.x = x;
        this.y = y;
    }
} 
时间: 2024-10-09 08:37:11

[leedcode 130] Surrounded Regions的相关文章

130. Surrounded Regions

Given a 2D board containing 'X' and 'O' (the letter 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, th

leetcode 130 Surrounded Regions(BFS)

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

130. Surrounded Regions (Graph; DFS)

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

【一天一道LeetCode】#130. Surrounded Regions

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

130. 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

Java for LeetCode 130 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 X After running your function, the board should be:

130. Surrounded Regions(周围区域问题 广度优先)(代码未完成!!)

Given a 2D board containing 'X' and 'O' (the letter 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, th

[LeetCode]130 Surrounded Regions(DFS)

题目链接:https://leetcode.com/problems/surrounded-regions/?tab=Description 题意:把非边界的O改成X. 先dfs边界的,打好标记.把没变的变成X后同时更新标记的为O. 1 class Solution { 2 public: 3 const int dx[5] = {0, 0, 1, -1}; 4 const int dy[5] = {1, -1, 0, 0}; 5 int n, m; 6 bool ok(int x, int y

leetcode[130] Surrounded Regions

给定一个类似棋盘,有X和O,把X圈住的O变为X例如: 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 其实这题的思路,就是先找到四周的O,然后和四周的O相邻的O全都是不可能转化为X的,我们用另外一个符号标记,那么最后,矩阵中的O就是被圈住的,就需要转化为X,然后其他标记的符号就要转为原来的O.