LeetCode - Surrounded Regions

2 minute read

Problem description

description

Given a 2D board containing ‘X’ and ‘O’ (the letter O), capture all regions surrounded by ‘X’.

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

Example:

1
2
3
4
X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

1
2
3
4
X X X X
X X X X
X X X X
X O X X

Explanation:

Surrounded regions shouldn’t 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.

Analysis

The basic idea is to first find the ‘O’ shouldn’t be changed. And then change all other ‘O’.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution {
    public void solve(char[][] board) {
        if (board == null){
            return;
        }
        int row = board.length;
        if (row == 0){
            return;
        }
        int col = board[0].length;
        if (col == 0){
            return;
        }
        
        
        int[][] visited = new int[row][col]; // 0 is unvisited, 1 is visited and true, 2 is visited and false;
        
        for (int i = 0; i< row;i++){
            if (board[i][0] == 'O'){
                dfs(i,0, board, 'O', 'O', visited);
            }
            
            if (board[i][col-1] == 'O'){
                dfs(i,col-1, board, 'O', 'O', visited);
            }
        }
        
        for (int i = 0; i< col;i++){
            if (board[0][i] == 'O'){
                dfs(0, i, board, 'O', 'O', visited);
            }
            
            if (board[row-1][i] == 'O'){
                dfs(row-1, i, board, 'O', 'O', visited);
            }
        }
        
        for (int i = 0; i < row; i++){
            for (int j = 0; j < col; j++){
                if (board[i][j] == 'O' && visited[i][j] == 0){
                    board[i][j] = 'X';
                    // dfs(i,j,board, 'O', 'X', visited);
                }
            }
        }
    }
    
    int[][] dir = new int[][]{ {1,0}, {0,1}, {-1,0}, {0,-1} };
    
    void dfs(int i, int j, char[][] board, char from, char to, int[][] visited){
        if (i < 0 || j < 0 || i >= board.length || j >= board[0].length || visited[i][j] != 0){
            return;
        }
        
        visited[i][j] = 2;
        
        if (board[i][j] == from){
            board[i][j] = to;
            visited[i][j] = 1;
            
            for (int[] d:dir){
                dfs(i+d[0], j+d[1], board, from, to, visited);
            }
        }
    }
}

What to improve

  • for the second iteration, we can just iterate all the cell but not use dfs again since we alread know the ‘O’ we find is the one to change.