0%

130. Surrounded Regions

130. Surrounded Regions

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
class Solution {
public:
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
for(int i = 0; i < n; ++i)
{
goNeighbor(board, 0, i);
goNeighbor(board, m - 1, i);
}
for(int i = 0; i < m; ++i)
{
goNeighbor(board, i, 0);
goNeighbor(board, i, n - 1);
}
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '1')
board[i][j] = 'O';
}
}
private:
void goNeighbor(vector<vector<char>>& board, int i, int j)
{
if(board[i][j] != 'O')
return;
board[i][j] = '1';
if(i > 0)
goNeighbor(board, i - 1, j);
if(i < board.size() - 1)
goNeighbor(board, i + 1, j);
if(j > 0)
goNeighbor(board, i, j - 1);
if(j < board[0].size() - 1)
goNeighbor(board, i, j + 1);
}
};

当且仅当和边界相连的O块区域才可能被表留,而在中间的就全都被覆盖,因此先沿着边界检查,把所有这种O区域改成1表示要保留的,然后把里面剩下的O改成X,1改回O就行了

使用迭代 BFS?
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
class Solution {
public:
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
queue<pair<int, int>> q;
for(int i = 0; i < m; ++i)
{
for(int j = 0; j < n; ++j)
{
if(j != 0 && i != 0 && j != n - 1 && i != m - 1)
continue;
q.push(make_pair(i, j));
while(!q.empty())
{
auto b = q.front();
q.pop();
int i = b.first;
int j = b.second;
if(board[i][j] == 'O')
{
board[i][j] = '1';
if(i > 1)
q.push(make_pair(i - 1, j));
if(i < m - 2)
q.push(make_pair(i + 1, j));
if(j > 1)
q.push(make_pair(i, j - 1));
if(j < n - 2)
q.push(make_pair(i, j + 1));
}
}
}
}
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '1')
board[i][j] = 'O';
}
}
};

关于stackoverflow https://leetcode.com/problems/surrounded-regions/discuss/41612/A-really-simple-and-readable-C++-solutionuff0conly-cost-12ms/39811

每个递归调用32B存储空间(1个int)(有待考证),然后250*2501623589017459.png

就占用32 * 250 * 250 / 2 = 1000000B = 1M = 栈大小

review

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
class Solution {
public:
void solve(vector<vector<char>>& board) {
int m = board.size(), n = board[0].size();
queue<pair<int, int>> q;
for(int i = 0; i < m; ++i)
{
dfsIt(board, m, n, i, 0);
dfsIt(board, m, n, i, n - 1);
}
for(int i = 0; i < n; ++i)
{
dfsIt(board, m, n, 0, i);
dfsIt(board, m, n, m - 1, i);
}
for(int i = 0; i < m; ++i)
for(int j = 0; j < n; ++j)
if(board[i][j] != '#')
board[i][j] = 'X';
else if(board[i][j] == '#')
board[i][j] = 'O';
}
private:
inline void dfsIt(vector<vector<char>>& board, int m, int n, int i, int j)
{
queue<pair<int, int>> q;
q.emplace(i, j);
while(!q.empty())
{
int i, j;
tie(i, j) = q.front();
q.pop();
if(i < 0 || i >= m || j < 0 || j >=n)
continue;
if(board[i][j] == 'O')
{
board[i][j] = '#';
q.emplace(i + 1, j);
q.emplace(i - 1, j);
q.emplace(i, j + 1);
q.emplace(i, j - 1);
}
}
}
};