Question

Formatted question description: https://leetcode.ca/all/130.html

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

Algorithm

It’s a bit like “Go” chess, turning all the enclosed O into X, but the difference is that the O on the edge is not counted as being captured.

It is similar to the previous number of Islands and can be solved by DFS. At the beginning, my idea was that DFS traverses the O in the middle. If it does not reach the edge, it becomes X. If it reaches the edge, it changes back to the X before it.

However, it is very expensive to do this. The common practice seen on the Internet is to scan the four sides of the matrix. If there is an O, use DFS to traverse and change all consecutive Os into another character, such as $, so that the remaining The next Os are all captured, then change these Os into X, and change the $ back to O.

Code

Java

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

public class Surrounded_Regions {

	 public class Solution_bfs {

	     // record visited positions
	     HashSet<String> hs = new HashSet<String> ();

	     // BFS, using queue
	     public void solve(char[][] board) {
	         if (board == null || board.length == 0)     return;

	         int row = board.length;
	         int column = board[0].length;

	         // search each edge
	         for (int i = 0; i < row; i++) {
	             for (int j = 0; j < column; j++) {

	                 // find edge
	                 if (i == 0 || i == row - 1 || j  == 0 || j == column - 1) {

	                     if (board[i][j] == 'O') {

	                         // key is compressed as "i_j"
	                         if (!hs.contains(getKey(i, j))) {

	                             // @note: like doing add when enqueue, also dont add it here, add ONLY happens after dequeue
	                             // hs.add(i+"_"+j);
	                             BFS(board, i, j);
	                         }
	                     }
	                 }
	             }
	         }

	         // if a position is not in hashset, then its "X"
	         for (int i = 0; i < row; i++) {
	             for (int j = 0; j < column; j++) {

	                 if (!hs.contains(getKey(i, j)))
	                     board[i][j] = 'X';
	             }
	         }

	     }

	     public void BFS (char[][] board, int inow, int jnow) {

	         Queue<String> q = new LinkedList<String>();
	         q.offer(getKey(inow, jnow));

	         while (!q.isEmpty()) {
	             String keycode = q.poll();
	             String[] array = keycode.split("_");
	             int i = Integer.parseInt(array[0]), j = Integer.parseInt(array[1]);

	             if (board[i][j] == 'O' && !hs.contains(getKey(i, j))) {
	                 hs.add(getKey(i, j));

	                 // search neighbours of this position, find "O" and enqueue
	                 //@note: dont consider positions on edge, since they are safe. just enqueue those NOT-edge
	                 //      I was messed up by checking elements on edge, but no need
	                 if (isValid(i + 1, j, board) && board[i + 1][j] == 'O' && !hs.contains(getKey(i + 1, j)))
	                     {
	                         q.offer(getKey(i + 1, j));

	                         /*
	                             @note: dont add to hashset here, if add here, next dequeue and get this one,
	                                     it's not going to search its neighbours since it is in hashset
	                         */
	                         // hs.add(getKey(i + 1, j));
	                     }

	                 if (isValid(i - 1, j, board) && board[i - 1][j] == 'O' && !hs.contains(getKey(i - 1, j)))
	                     {   q.offer(getKey(i - 1, j));  /* hs.add(getKey(i - 1, j));*/ }

	                 if (isValid(i, j + 1, board) && board[i][j + 1] == 'O' && !hs.contains(getKey(i, j + 1)))
	                     {   q.offer(getKey(i, j + 1));  /* hs.add(getKey(i, j + 1));*/ }

	                 if (isValid(i, j - 1, board) && board[i][j - 1] == 'O' && !hs.contains(getKey(i, j - 1)))
	                     {   q.offer(getKey(i, j - 1));  /* hs.add(getKey(i, j + 1));*/ }
	             }

	         }
	     }

	     public boolean isValid(int i, int j, char[][] board) {
	         int row = board.length;
	         int column = board[0].length;

	         if (i >= row || i < 0 || j >= column || j < 0)
	             return false;
	         else
	             return true;
	     }

	     public String getKey(int i, int j) {

	         return i+"_"+j;
	     }
	 }


	// re-write dfs, check validity before next recursion. no bird use
	public class Solution_dfs_improved {
	    public void solve(char[][] board) {

	        if (board.length == 0 || board[0].length == 0) {
	            return;
	        }
	        // if 'O' is somehow connected to boarder, then it's alive. NOT the same as GO chess

	        // if a single 'O' is alive, mark it as '.', then change it back to 'O' after flipping those trapped ones

	        int rowNum = board.length;
	        int colNum = board[0].length;

	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                // try dfs on all 'O's on 4 boarders
	                if ((i == 0 || i == rowNum - 1 || j == 0 || j == colNum - 1)
	                    && board[i][j] == 'O') { // @note: accelaration is, if previous dfs modified some 'O', then no repeat dfs
	                    // System.out.println("before one dfs: " + Arrays.deepToString(board));
	                    dfs(board, i, j);
	                }
	            }
	        }

	        // flip trapped 'O's
	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                if (board[i][j] == 'O') {
	                    board[i][j] = 'X';
	                }
	            }
	        }

	        // restore alive 'O's
	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                if (board[i][j] == '.') {
	                    board[i][j] = 'O';
	                }
	            }
	        }

	    }

        public void dfs(char[][] b, int i, int j) {

            if (b[i][j] == 'O') {
                b[i][j] = '.';
            }

            // isValid() will also check if visited
            if (isValid(b, i + 1, j) && b[i + 1][j] == 'O')     dfs(b, i + 1, j);
            if (isValid(b, i - 1, j) && b[i - 1][j] == 'O')     dfs(b, i - 1, j);
            if (isValid(b, i, j + 1) && b[i][j + 1] == 'O')     dfs(b, i, j + 1);
            if (isValid(b, i, j - 1) && b[i][j - 1] == 'O')     dfs(b, i, j - 1);
        }

        public boolean isValid(char[][] b, int i, int j) {
            int row = b.length, col = b[0].length;

            if (i >= row || i < 0 || j >= col || j < 0)
                return false;
            else
                return true;
        }
	}


	// stackoverflow at dfs
	public class Solution_dfs_over_limit {
	    public void solve(char[][] board) {

	        if (board.length == 0 || board[0].length == 0) {
	            return;
	        }
	        // if 'O' is somehow connected to boarder, then it's alive. NOT the same as GO chess

	        // if a single 'O' is alive, mark it as '.', then change it back to 'O' after flipping those trapped ones

	        int rowNum = board.length;
	        int colNum = board[0].length;

	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                // try dfs on all 'O's on 4 boarders
	                if ((i == 0 || i == rowNum - 1 || j == 0 || j == colNum - 1)
	                    && board[i][j] == 'O') { // @note: accelaration is, if previous dfs modified some 'O', then no repeat dfs
	                    dfs(board, i, j);
	                }
	            }
	        }

	        // flip trapped 'O's
	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                if (board[i][j] == 'O') {
	                    board[i][j] = 'X';
	                }
	            }
	        }

	        // restore alive 'O's
	        for (int i = 0; i < rowNum; i++) {
	            for (int j = 0; j < colNum; j++) {

	                if (board[i][j] == '.') {
	                    board[i][j] = 'O';
	                }
	            }
	        }

	    }

	    private void dfs(char[][] board, int i, int j) {

	        if (i < 0 || j < 0 || i >= board.length || j >= board[0].length) {
	            return;
	        }

	        // if it's 'X', dont't do dfs
	        if (board[i][j] != 'O') { // @note: i missed thsi check, and fill full board with '.' ...
	            return;
	        }

	        board[i][j] = '.';

	        dfs(board, i - 1, j);
	        dfs(board, i + 1, j);
	        dfs(board, i, j - 1);
	        dfs(board, i, j + 1);
	    }
	}
}

All Problems

All Solutions