Skip to content

Commit 6cc8ff3

Browse files
committed
130
1 parent b4ddaba commit 6cc8ff3

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Diff for: solutions/130.Surrounded_Regions/AC_bfs_n.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Author: illuz <iilluzen[at]gmail.com>
3+
* File: AC_bfs_n.cpp
4+
* Create Date: 2015-03-14 21:36:32
5+
* Descripton: BFS, update O in origin map.
6+
*/
7+
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
const int N = 0;
12+
13+
class Solution {
14+
private:
15+
const int dx[4] = {0, 0, 1, -1};
16+
const int dy[4] = {1, -1, 0, 0};
17+
void bfs(int x, int y, vector<vector<char> > &board) {
18+
int n = board.size(), m = board[0].size();
19+
queue<pair<int, int> > q;
20+
q.push(make_pair(x, y));
21+
board[x][y] = '+';
22+
23+
while (!q.empty()) {
24+
x = q.front().first;
25+
y = q.front().second;
26+
q.pop();
27+
// go to 4 directions
28+
for (int i = 0; i < 4; ++i) {
29+
int nx = x + dx[i], ny = y + dy[i];
30+
if (nx >= 0 && nx < n && ny >= 0 && ny < m && board[nx][ny] == 'O') {
31+
board[nx][ny] = '+';
32+
q.push(make_pair(nx, ny));
33+
}
34+
}
35+
}
36+
}
37+
38+
public:
39+
void solve(vector<vector<char>> &board) {
40+
if (board.empty() || board[0].empty())
41+
return;
42+
43+
int n = board.size(), m = board[0].size();
44+
if (n <= 2 || m <= 2)
45+
return;
46+
47+
// find side O
48+
for (int i = 0; i < n; ++i) {
49+
if (board[i][0] == 'O')
50+
bfs(i, 0, board);
51+
if (board[i][m - 1] == 'O')
52+
bfs(i, m - 1, board);
53+
}
54+
for (int j = 0; j < m; ++j) {
55+
if (board[0][j] == 'O')
56+
bfs(0, j, board);
57+
if (board[n - 1][j] == 'O')
58+
bfs(n - 1, j, board);
59+
}
60+
61+
// change board
62+
for (int i = 0; i < n; ++i)
63+
for (int j = 0; j < m; ++j)
64+
if (board[i][j] == '+')
65+
board[i][j] = 'O';
66+
else if (board[i][j] == 'O')
67+
board[i][j] = 'X';
68+
69+
}
70+
};
71+
72+
int main() {
73+
int n;
74+
cin >> n;
75+
vector<vector<char> > inp(n);
76+
for (auto &i : inp) {
77+
string str;
78+
cin >> str;
79+
for (auto & j : str)
80+
i.push_back(j);
81+
}
82+
Solution s;
83+
s.solve(inp);
84+
return 0;
85+
}
86+

Diff for: solutions/130.Surrounded_Regions/readme.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## 130. Surrounded Regions (Medium)
2+
3+
### **链接**
4+
题目:https://leetcode.com/problems/surrounded-regions/
5+
代码(github):https://github.com/illuz/leetcode
6+
7+
### **题意**
8+
给一个地图,把地图里被 X 包围的 O 块填充为 X。
9+
10+
### **分析**
11+
12+
1. 找到 O 点就 DFS 填充。不过 DFS 递归是要占用栈的,最糟的空间复杂度是 O(n*n),这题会卡爆栈,所以 DFS 会 Memory Limit Exceed。
13+
2. 找到 O 点就 BFS 填充。BFS 才是正解。
14+
15+
不过一找到 O 就开始处理的话,如果来个全是 O 的,时间复杂度就会变成 O(n*n) 了,就会 TLE,有两种解决方案:1. 开个数组来记录是否访问过,需要 O(n*n) 的空间;2. 遍历 O 的时候把遍历过的 O 而不用变 X 的点变成 +,全部处理后再变回来,这样就不用另开空间了。
16+
17+
在上面的“把遍历过的 O 而不用变 X 的点变成 +”这个方法中,如果正常去做就要先来一遍 BFS 看看有没有被包围,再一遍 BFS 变点。能不能把两遍 BFS 缩成一遍呢?其实是可以的,我们还是把不用变的化作 '+',那反过来想,我们只要处理需要化作 '+' 的点不就行了,找到边界的点再 BFS 把 O 变成 +,最后处理时还是 O 的就是可以填充的点了。

0 commit comments

Comments
 (0)