[Programming Problem] Surrounded Regions

Given an m x n matrix board containing ‘X’ and ‘O’, capture all regions that are 4-directionally surrounded by ‘X’.

A region is captured by flipping all ‘O’s into ‘X’s in that surrounded region.

Example 1:
Input: board = [[“X”,”X”,”X”,”X”],[“X”,”O”,”O”,”X”],[“X”,”X”,”O”,”X”],[“X”,”O”,”X”,”X”]]
Output: [[“X”,”X”,”X”,”X”],[“X”,”X”,”X”,”X”],[“X”,”X”,”X”,”X”],[“X”,”O”,”X”,”X”]]
Explanation: Surrounded regions should not be on the border, which means that any ‘O’ on the border of the board are not flipped to ‘X’. Any ‘O’ that is not on the border and it is not connected to an ‘O’ on the border will be flipped to ‘X’. Two cells are connected if they are adjacent cells connected horizontally or vertically.
Example 2:

Input: board = [[“X”]]
Output: [[“X”]]

[Problem Link]

The high level idea here is to traverse the borders and run the floodfill algorithm to identify regions touching borders. Once we’ve identified those, we’ve identified regions we should leave alone vs. regions we need to flip. To do this we:-

  1. Make a copy of the board
  2. Run floodfill algorithm on: top and bottom row, first and last column
  3. After floodfill algorithm, all border islands will be marked as ‘-‘
  4. We then traverse our (copied) board to set appropriate values on board (depending on output from floodfill algorithm). We set all border islands (depicted by ‘-‘) as ‘O’ and inner islands (depicted by ‘O’) set as ‘X’
/**
 * @param {character[][]} board
 * @return {void} Do not return anything, modify board in-place instead.
 */
var solve = function(board) {
    // make a copy of the board
    const boardCopy = board.map((x) => x.map((y) => y));
 
    // Run floodfill algorithm on: top row and botton row
    for ( let j = 0 ; j < boardCopy[0].length ; j++ ) {
        if (boardCopy[0][j] === 'O') floodFill(boardCopy, 0, j);
        if (boardCopy[boardCopy.length-1][j] === 'O') floodFill(boardCopy, boardCopy.length-1, j);
    }
 
    // Run floodfill algorithm on: first and last column
    for ( let i = 0 ; i < boardCopy.length ; i++ ) {
        if (boardCopy[i][0] === 'O') floodFill(boardCopy, i, 0);
        if (boardCopy[i][boardCopy[0].length-1] === 'O') floodFill(boardCopy, i, boardCopy[0].length-1);
    }
 
    // After floodfill algorithm, all border islands will be marked as '-'
    // We basically set all border islands as 'O'
    // Other values with 'O' (which are inner islands) will be set as 'X'    
    // Set appropriate values on board (depending on output from floodfill algorithm)
    boardCopy.forEach((x, i) => {
        return x.forEach((y, j) => {
            if (y === '-') {
                board[i][j] = 'O';
            } else if (y === 'O') {
                board[i][j] = 'X'
            } else { board[i][j] = y };
        })
    })
 
    return board;
};
 
var floodFill = function(board, i, j) {
    if ( i < 0 || i >= board.length || j < 0 || j >= board[0].length ) return;
 
    if (board[i][j] === 'O') {
        board[i][j] = '-';
        floodFill(board, i+1, j);
        floodFill(board, i-1, j);
        floodFill(board, i, j+1);
        floodFill(board, i, j-1);        
    }
}

3 thoughts on “[Programming Problem] Surrounded Regions

Leave a Reply

Your email address will not be published.