[LeetCode] Surrounded Regions

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

思路

原本想用dfs作,代码感受比bfs感受简单多了,结果作出来是stackoverflow的,要加上限制条件,才能跑过。遇到这种图问题仍是要先肯定一下图的大小,再肯定用BFS仍是DFScode

代码

public void solve(char[][] board) {
    //corner case
    if(board == null || board.length <= 1 || board[0] == null || board[0].length <= 1) return;
    int rows = board.length, cols = board[0].length;
    
    //flip all adjacent boarder O into Y, the first col
    for(int i = 0; i < rows; i++){
        if(board[i][0] == 'O'){
            helper(board, rows, cols, i, 0);
        }
    }
    //flip all adjacent boarder O into Y, the first row
    for(int j = 0; j < cols; j++){
        if(board[0][j] == 'O'){
            helper(board, rows, cols, 0, j);
        }
    }
    
    //flip all adjacent boarder O into Y, the last col
    for(int i = 0; i < rows; i++){
        if(board[i][cols - 1] == 'O'){
            helper(board, rows, cols, i, cols - 1);
        }
    }
    
    //flip all adjacent boarder O into Y, the last row
    for(int j = 0; j < cols; j++){
        if(board[rows - 1][j] == 'O'){
            helper(board, rows, cols, rows - 1, j);
        }
    }
   
    //flip all 'Y' into 'O', flip all 'O' into 'X'
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            if(board[i][j] == 'Y'){
                board[i][j] = 'O';
            }
            else if(board[i][j] =='O'){
                board[i][j] = 'X';
            }
        }
    }
}

private void helper(char[][] board, int rows, int cols, int i, int j){
    //base case
    if(i < 0 || i >= rows || j < 0 || j >= cols || board[i][j] != 'O'){
        return;
    }
    board[i][j] = 'Y';
    //go up
    if(i < rows - 2) helper(board, rows, cols, i + 1, j);
    //go down
    if(i > 1) helper(board, rows, cols, i - 1, j);
    //go left
    if(j > 1) helper(board, rows, cols, i, j - 1);
    //go right
    if(j < cols - 2) helper(board, rows, cols, i, j + 1);
}

BFS

思路

把四周的是‘O’的点所有加入queue, 把与它相连的'O'再所有加入queue,翻转成'#'
最后再所有遍历一遍,把’#'变成’O‘,’O‘变成’X'ip

代码

class Point{
    //fields
    private int x;  //the row of the Point
    private int y;  //the col of the Point
    
    //methods
    public Point(int x, int y){
        this.x = x;
        this.y = y;
    }
}
public void solve(char[][] board) {
    //corner case
    if(board == null || board.length <= 2 || board[0] == null || board[0].length <= 2) return;
    int rows = board.length, cols = board[0].length;
    Queue<Point> queue = new LinkedList<Point>();
    
    for(int i = 0; i < rows; i++){
        if(board[i][0] == 'O'){
            queue.offer(new Point(i, 0));
            board[i][0] = '#';
        }
        if(board[i][cols - 1] == 'O' ){
            queue.offer(new Point(i, cols - 1));
            board[i][cols - 1] = '#';
        }
    }
    
    for(int j = 0; j < cols; j++){
        if(board[0][j] == 'O'){
            queue.offer(new Point(0, j));
            board[0][j] = '#';
        }
        if(board[rows - 1][j] == 'O'){
            queue.offer(new Point(rows - 1, j));
            board[rows - 1][j] = '#';
        }
    }

    int[][] directions = new int[][]{{-1,0},{1,0},{0,1},{0,-1}};
    while(!queue.isEmpty()){
        Point cur = queue.poll();
        for(int k = 0; k < 4; k++){
            int row = cur.x + directions[k][0];
            int col = cur.y + directions[k][1];
            if(row >= 0 && row < rows && col >= 0 && col < cols && board[row][col] == 'O'){
                board[row][col] = '#';
                queue.offer(new Point(row, col));
            }
        }
    }
    
    //flip
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < cols; j++){
            if(board[i][j] == '#'){
                board[i][j] = 'O';
            }
            else if(board[i][j] == 'O'){
                board[i][j] = 'X';
            }
        }
    }
}
相关文章
相关标签/搜索