[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;  
    }  
} 

 

posted @ 2015-07-26 12:55  ~每天进步一点点~  阅读(140)  评论(0编辑  收藏  举报