forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130.Surrounded-Regions.cpp
75 lines (69 loc) · 1.74 KB
/
130.Surrounded-Regions.cpp
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
74
75
class Solution {
unordered_map<int,int>Father;
int M;
int N;
public:
void solve(vector<vector<char>>& board)
{
M=board.size();
if (M==0) return;
N=board[0].size();
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
{
if (board[i][j]=='X') continue;
if (i==0 || i==M-1 || j==0 || j==N-1)
Father[i*N+j]=-1;
else
Father[i*N+j]=i*N+j;
}
vector<pair<int,int>>dir={{1,0},{-1,0},{0,1},{0,-1}};
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
{
if (board[i][j]=='X') continue;
for (int k=0; k<4; k++)
{
int x=i+dir[k].first;
int y=j+dir[k].second;
if (x<0||x>=M||y<0||y>=N) continue;
if (board[x][y]=='X') continue;
if (FindSet(x*N+y)!=FindSet(i*N+j))
Union(x*N+y,i*N+j);
}
}
for (int i=0; i<M; i++)
for (int j=0; j<N; j++)
{
if (board[i][j]=='X') continue;
if (FindSet(i*N+j)!=-1)
board[i][j]='X';
}
}
int FindSet(int x)
{
if (Father[x]==-1)
return -1;
if (Father[x]!=x)
{
Father[x]=FindSet(Father[x]);
}
return Father[x];
}
void Union(int x,int y)
{
x=Father[x];
y=Father[y];
bool flag;
if (x/N<y/N)
flag=1;
else if (x%N==y%N)
flag=1;
else
flag=0;
if (flag)
Father[y]=x;
else
Father[x]=y;
}
};