2013年9月15日星期日

Leetcoder - 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 visit(int i, int j, char[][] board, boolean visited[][], boolean exempt[][], Stack<int[]> tovisit) {
        if(i<0 || i>=row || j<0 || j>=column) {
            return;
        }
        if(visited[i][j]) {
            return;
        }
        visited[i][j] = true;
        if(board[i][j] == 'O') {
            int cell[] = new int[2];
            cell[0] = i;
            cell[1] = j;
            tovisit.add(cell);
            exempt[i][j] = true;
        }
    }
 
    int row;
    int column;
 
    public void solve(char[][] board) {
        // Start typing your Java solution below
        // DO NOT write main() function
        // O in 4 boards cannot be captured
        row = board.length;
        if(row==0) {
            return;
        }
        column = board[0].length;
        if(column==0) {
            return;
        }
        boolean[][] visited = new boolean[row][column];
     
        boolean[][] exempt = new boolean[row][column];
     
        Stack<int[]> tovisit = new Stack<int[]>();
     
        for(int i=0;i<row;i++) {
            visit(i, 0, board, visited, exempt, tovisit);
            visit(i, column-1, board, visited, exempt, tovisit);
        }
     
        for(int j=1;j<column-1;j++) {
            visit(0, j, board, visited, exempt, tovisit);
            visit(row-1, j, board, visited, exempt, tovisit);
        }
     
        while(!tovisit.isEmpty()) {
            int[] cell = tovisit.pop();
            int i = cell[0];
            int j = cell[1];
            visit(i+1, j, board, visited, exempt, tovisit);
            visit(i-1, j, board, visited, exempt, tovisit);
            visit(i, j+1, board, visited, exempt, tovisit);
            visit(i, j-1, board, visited, exempt, tovisit);
        }
     
        for(int i=0;i<row;i++) {
            for(int j=0;j<column;j++) {
                if(board[i][j]=='O' && !exempt[i][j]) {
                    board[i][j] = 'X';
                }
            }
        }
    }
}



public class Solution {
    public void solve(char[][] board) {
        if(board==null || board.length==0 || board[0].length==0) return;
        int m = board.length;
        int n = board[0].length;
       
        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);
        }
       
        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);
        }
       
        for(int i=0;i<m;i++) {
            for(int j=0;j<n;j++) {
                if(board[i][j]=='+') board[i][j] = 'O';
                else if(board[i][j]=='O') board[i][j] = 'X';
            }
        }
    }
   
    private void bfs(char[][] board, int i, int j) {
        board[i][j] = '+';
        LinkedList<int[]> lst = new LinkedList<int[]>();
        int m = board.length;
        int n = board[0].length;
        int[] idx = new int[2];
        idx[0] = i;
        idx[1] = j;
        lst.add(idx);
        while(lst.size()!=0) {
            int[] tmp = lst.removeFirst();
            int x = tmp[0];
            int y = tmp[1];
            add(x-1, y, board, lst);
            add(x+1, y, board, lst);
            add(x, y-1, board, lst);
            add(x, y+1, board, lst);
        }
    }
   
    private void add(int i, int j, char[][] board, LinkedList<int[]> lst) {
        int m = board.length;
        int n = board[0].length;
        if(i<0 || i>=m || j<0 || j>=n || board[i][j]!='O') return;
        int[] idx = new int[2];
        idx[0] = i;
        idx[1] = j;
        lst.addFirst(idx);
        board[i][j] = '+';
    }
}


========================================================

public class Solution {
    public void solve(char[][] board) {
        if (board == null || board.length == 0 || board[0].length == 0) {
            return;
        }
        int row = board.length;
        int column = board[0].length;
       
        for (int i = 0; i < row; i++) {
            if (board[i][0] == 'O') {
                tag(board, i, 0);
            }
            if (board[i][column - 1] == 'O') {
                tag(board, i , column - 1);
            }
        }
        for (int i = 1; i < column - 1; i++) {
            if (board[0][i] == 'O') {
                tag(board, 0, i);
            }
            if (board[row - 1][i] == 'O') {
                tag(board, row - 1, i);
            }
        }
       
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (board[i][j] == '+') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }
   
    private void tag(char[][] board, int i, int j) {
        int row = board.length;
        int column = board[0].length;
        if (board[i][j] == 'O') {
            board[i][j] = '+';
            if (i - 1 > 0) tag(board, i - 1, j);
            if (i + 1 < row) tag(board, i + 1, j);
            if (j - 1 > 0) tag(board, i, j - 1);
            if (j + 1 < column) tag(board, i, j + 1);
        }
    }
}

没有评论:

发表评论