Saturday, January 10, 2015

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 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 {
    private Map<Character, Boolean> map;
    
    public void solve(char[][] board) {
    // Adopt breadth first search
    // 1. Set connected 'O's as a specific value (Use bfs to search the connected component);
    // 2. If any 'O' of them touches the boundary, they should be 'O';
    // 3. If not, they should be 'X'.
    // 4. Save result in HashMap.
        int m = board.length;
        
        if (m == 0)
            return;
            
        int n = board[0].length;
        
        map = new HashMap<>();
        
        char value = '1';
        
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (board[i][j] == 'O')
                {
                    boolean isX = bfs(board, i, j, value);
                    map.put(value, isX);
                    value++;
                }

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (board[i][j] != 'X')
                {
                    if (map.get(board[i][j]))
                        board[i][j] = 'X';
                    else
                        board[i][j] = 'O';
                }   
    }
    
    private boolean bfs(char[][] board, int x, int y, char val)
    {
        boolean isX = true;
        int m = board.length;
        int n = board[0].length;
        
        Queue<Integer> xC = new LinkedList<>();
        Queue<Integer> yC = new LinkedList<>();
        
        xC.offer(x);
        yC.offer(y);
        board[x][y] = val;
        
        while (!xC.isEmpty() && !yC.isEmpty())
        {
            int row = xC.poll();
            int col = yC.poll();
            
            // If touch the boundary
            if (row==0 || row==m-1 || col==0 || col==n-1)
                isX = false;
            
            if (row-1>=0 && board[row-1][col]=='O')
            {
                xC.offer(row-1);
                yC.offer(col);
                board[row-1][col] = val;
            }
            
            if (row+1<m && board[row+1][col]=='O')
            {
                xC.offer(row+1);
                yC.offer(col);
                board[row+1][col] = val;
            }
            
            if (col-1>=0 && board[row][col-1]=='O')
            {
                xC.offer(row);
                yC.offer(col-1);
                board[row][col-1] = val;
            }            
            
            if (col+1<n && board[row][col+1]=='O')
            {
                xC.offer(row);
                yC.offer(col+1);
                board[row][col+1] = val;
            }
        }
        
        return isX;
    }
}

No comments:

Post a Comment