Leetcode Study Day 43

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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: Notice that an 'O' should not be flipped if:
- It is on the border, or
- It is adjacent to an 'O' that should not be flipped.
The bottom 'O' is on the border, so it is not flipped.
The other three 'O' form a surrounded region, so they are flipped.
Example 2:

Input: board = [["X"]]
Output: [["X"]]


Constraints:

m == board.length
n == board[i].length
1 <= m, n <= 200
board[i][j] is 'X' or 'O'.

Solution

The solution is like the island problem. We use DFS to solve this question.

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
67
68
69
70
71
72
73
class Solution {
public:
void DFS(vector<vector<char>>& board, int i, int j, int m, int n) {
if(i<0 or j<0 or i>=m or j>=n or board[i][j] != 'O') return;
board[i][j] = '#';
DFS(board, i-1, j, m, n);
DFS(board, i+1, j, m, n);
DFS(board, i, j-1, m, n);
DFS(board, i, j+1, m, n);
}

void solve(vector<vector<char>>& board) {

//We will use boundary DFS to solve this problem

// Let's analyze when an 'O' cannot be flipped,
// if it has atleast one 'O' in it's adjacent, AND ultimately this chain of adjacent 'O's is connected to some 'O' which lies on boundary of board

//consider these two cases for clarity :
/*
O's won't be flipped O's will be flipped
[X O X X X] [X X X X X]
[X O O O X] [X O O O X]
[X O X X X] [X O X X X]
[X X X X X] [X X X X X]

So we can conclude if a chain of adjacent O's is connected some O on boundary then they cannot be flipped

*/

//Steps to Solve :
//1. Move over the boundary of board, and find O's
//2. Every time we find an O, perform DFS from it's position
//3. In DFS convert all 'O' to '#' (why?? so that we can differentiate which 'O' can be flipped and which cannot be)
//4. After all DFSs have been performed, board contains three elements,#,O and X
//5. 'O' are left over elements which are not connected to any boundary O, so flip them to 'X'
//6. '#' are elements which cannot be flipped to 'X', so flip them back to 'O'
//7. finally, Upvote the solution😊


int m = board.size();

if(m == 0) return;

int n = board[0].size();

//Moving over firts and last column
for(int i=0; i<m; i++) {
if(board[i][0] == 'O')
DFS(board, i, 0, m, n);
if(board[i][n-1] == 'O')
DFS(board, i, n-1, m, n);
}


//Moving over first and last row
for(int j=0; j<n; j++) {
if(board[0][j] == 'O')
DFS(board, 0, j, m, n);
if(board[m-1][j] == 'O')
DFS(board, m-1, j, m, n);
}

for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
if(board[i][j] == '#')
board[i][j] = 'O';
}
}
};
  • Copyrights © 2020-2024 Yangyang Cui

请我喝杯咖啡吧~

支付宝
微信