Skip to content

Commit 3057971

Browse files
Add files via upload
1 parent 280c5e0 commit 3057971

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// 解题思路依然使用并查集,特殊点在于使用了一个虚拟的父亲节点将所有边界上、以及边界上的邻居点都串起来了
2+
3+
// Runtime: 24 ms, faster than 98.26% of C++ online submissions for Surrounded Regions.
4+
// Memory Usage: 14.4 MB, less than 34.94% of C++ online submissions for Surrounded Regions.
5+
6+
class Solution
7+
{
8+
public:
9+
void solve(vector<vector<char>>& board)
10+
{
11+
// 边界条件处理
12+
rows = board.size();
13+
14+
if (rows == 0)
15+
{
16+
return ;
17+
}
18+
19+
cols = board[0].size();
20+
21+
if (cols == 0)
22+
{
23+
return ;
24+
}
25+
26+
// 构建并查集
27+
vector<pair<int, int>> unionTree(rows * cols + 1);
28+
29+
// 初始化并查集 depth parent
30+
for (int i = 0; i < unionTree.size(); i++)
31+
{
32+
unionTree[i].first = 1;
33+
unionTree[i].second = i;
34+
}
35+
36+
// 构建并查集
37+
for (int i = 0; i < rows; i++)
38+
{
39+
for (int j = 0; j < cols; j++)
40+
{
41+
if (board[i][j] == 'O')
42+
{
43+
int index1 = i * cols + j;
44+
45+
// 如果目标在边界上
46+
if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1)
47+
{
48+
int index2 = rows * cols;
49+
unionx(index1, index2, unionTree);
50+
}
51+
52+
// 和上边的数据相连
53+
if (i - 1 >= 0 && board[i - 1][j] == 'O')
54+
{
55+
int index2 = (i - 1) * cols + j;
56+
unionx(index1, index2, unionTree);
57+
}
58+
59+
// 和左边的数据相连
60+
if (j - 1 >= 0 && board[i][j - 1] == 'O')
61+
{
62+
int index2 = i * cols + (j - 1);
63+
unionx(index1, index2, unionTree);
64+
}
65+
66+
}
67+
}
68+
}
69+
70+
// 根据构建好的并查集来翻转数据
71+
for (int i = 0; i < rows; i++)
72+
{
73+
for (int j = 0; j < cols; j++)
74+
{
75+
if (board[i][j] == 'O')
76+
{
77+
if (findParent(i * cols + j, unionTree) != findParent(rows * cols, unionTree))
78+
{
79+
board[i][j] = 'X';
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
private:
87+
88+
// 所能接受的最大查询路径长度
89+
int maxPathLength = 3;
90+
int rows;
91+
int cols;
92+
93+
// 合并函数
94+
void unionx(int index1, int index2, vector<pair<int, int>>& unionTree)
95+
{
96+
int parent1 = findParent(index1, unionTree);
97+
int parent2 = findParent(index2, unionTree);
98+
99+
if (parent1 == parent2)
100+
{
101+
return ;
102+
}
103+
104+
// 将高度较低的树向高度较高的树进行合并
105+
if (unionTree[parent1].first > unionTree[parent2].first)
106+
{
107+
unionTree[parent2].second = parent1;
108+
}
109+
else
110+
{
111+
unionTree[parent1].second = parent2;
112+
113+
if (unionTree[parent1].first == unionTree[parent2].first)
114+
{
115+
unionTree[parent2].first++;
116+
}
117+
}
118+
}
119+
120+
// 查找父亲节点
121+
// 如果需要进一步加速就要使用路径压缩算法
122+
int findParent(int index, vector<pair<int, int>>& unionTree)
123+
{
124+
int initIndex = index;
125+
int pathLength = 0;
126+
127+
while (unionTree[index].second != index && unionTree[index].second != -1)
128+
{
129+
pathLength++;
130+
index = unionTree[index].second;
131+
}
132+
133+
if (pathLength > maxPathLength)
134+
{
135+
while (initIndex != index)
136+
{
137+
int temp = initIndex;
138+
initIndex = unionTree[initIndex].second;
139+
unionTree[temp].second = index;
140+
}
141+
}
142+
143+
return index;
144+
}
145+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Runtime: 256 ms, faster than 11.29% of Python3 online submissions for Surrounded Regions.
2+
# Memory Usage: 18.4 MB, less than 17.83% of Python3 online submissions for Surrounded Regionws.
3+
4+
class Solution:
5+
def __init__(self):
6+
self.unionTree = None
7+
self.maxPathLength = 3
8+
9+
def solve(self, grid: List[List[str]]) -> None:
10+
"""
11+
Do not return anything, modify board in-place instead.
12+
"""
13+
14+
rows = len(grid)
15+
16+
if rows is 0:
17+
return 0
18+
19+
cols = len(grid[0])
20+
if cols is 0:
21+
return 0
22+
23+
self.unionTree = [[1, index] for index in range(rows * cols + 1)]
24+
25+
for i in range(rows):
26+
for j in range(cols):
27+
if grid[i][j] == 'O':
28+
29+
index1 = i * cols + j
30+
31+
if (i == 0 or i == rows - 1 or j == 0 or j == cols - 1):
32+
index2 = rows * cols
33+
self.union(index1, index2)
34+
35+
if i - 1 >= 0 and grid[i - 1][j] == '1':
36+
index2 = (i - 1) * cols + j
37+
self.union(index1, index2)
38+
39+
if j - 1 >= 0 and grid[i][j - 1] == '1'":
40+
index2 = i * cols + (j - 1)
41+
self.union(index1, index2)
42+
43+
44+
for i in range(rows):
45+
for j in range(cols):
46+
if grid[i][j] == 'O':
47+
if self.findParent(i * cols + j) != self.findParent(rows * cols):
48+
grid[i][j] = 'X'
49+
50+
51+
def union(self, index1, index2):
52+
parent1 = self.findParent(index1)
53+
parent2 = self.findParent(index2)
54+
55+
if parent1 == parent2:
56+
return
57+
58+
if self.unionTree[parent1][0] > self.unionTree[parent2][0]:
59+
self.unionTree[parent2][1] = parent1
60+
else:
61+
self.unionTree[parent1][1] = parent2
62+
63+
if self.unionTree[parent1][0] == self.unionTree[parent2][0]:
64+
self.unionTree[parent2][0] += 1
65+
66+
def findParent(self, index):
67+
int initIndex = index
68+
pathLength = 0
69+
70+
while self.unionTree[index][1] != index:
71+
pathLength += 1
72+
index = self.unionTree[index][1]
73+
74+
if pathLength > self.maxPathLength:
75+
while initIndex != index:
76+
temp = initIndex
77+
initIndex = self.unionTree[initIndex][1]
78+
self.unionTree][temp][1] = index
79+
80+
return index

0 commit comments

Comments
 (0)