-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path130_Surrounded Regions.cpp
More file actions
151 lines (138 loc) · 4.6 KB
/
130_Surrounded Regions.cpp
File metadata and controls
151 lines (138 loc) · 4.6 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
// A region is captured by flipping all 'O's into 'X's in that surrounded region.
// For example,
// X X X X
// X O O X
// X X O X
// X O X X
// After running your function, the board should be:
// X X X X
// X X X X
// X X X X
// X O X X
// 解法:思路是将所有在边上的O,全部链接到一个同一个dummy root下。然后,每次都将
// O与它相邻的O点Union一下。最后遍历所有节点之后,所有与dummy root相连的点
// 为最终图中含有的O点。
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void solve(vector<vector<char>>& board) {
if (board.size() == 0 || board[0].size() == 0) return;
int row = board.size(), col = board[0].size();
// init the root array, set itself as its root
parent = new int[row * col + 1];
for (int i = 0; i < row * col + 1; ++i) parent[i] = i;
// union the O regions with their neighbors
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (board[i][j] == 'X') continue;
if (i == 0 || i == row - 1 || j == 0 || j == col - 1) unions(i * col + j, row * col);
if (i + 1 < row && board[i + 1][j] == 'O') unions(i * col + j, (i + 1) * col + j);
if (j + 1 < col && board[i][j + 1] == 'O') unions(i * col + j, i * col + (j + 1));
}
}
// modify the regions that are surrounded by X
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (find(i * col + j) != find(row * col)) board[i][j] = 'X';
}
}
}
void unions(int index1, int index2) {
parent[find(index1)] = find(index2);
}
int find(int index) {
while (parent[index] != index) {
parent[index] = parent[parent[index]];
index = parent[index];
}
return index;
}
private:
int *parent;
};
class Solution2 { // dfs
public:
void solve(vector<vector<char>>& board) {
int row, col;
// check corner cases
if (!board.empty() && !board[0].empty()) {
row = board.size(); col = board[0].size();
} else {
return;
}
// search the bound in board, and make changes in board2
vector<vector<char> > board2(row, vector<char>(col, 'X'));
for (int j = 0; j < col; ++j) {
if (board[0][j] == 'O') dfs(board, board2, 0, j, row, col);
if (board[row-1][j] == 'O') dfs(board, board2, row-1, j, row, col);
}
for (int i = 0; i < row; ++i) {
if (board[i][0] == 'O') dfs(board, board2, i, 0, row, col);
if (board[i][col-1] == 'O') dfs(board, board2, i, col-1, row, col);
}
// copy the result in board2 to board
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
board[i][j] = board2[i][j];
}
}
return;
}
void dfs(vector<vector<char> >& board, vector<vector<char> >& board2,
int i, int j, int& row, int& col) {
board[i][j] = 'X'; board2[i][j] = 'O';
// search in four directions
int di[4] = {0, 0, -1, 1};
int dj[4] = {1, -1, 0, 0};
for (int k = 0; k < 4; ++k) {
if (i + di[k] >= 0 && i + di[k] < row
&& j + dj[k] >= 0 && j + dj[k] < col
&& board[i + di[k]][j + dj[k]] == 'O') {
dfs(board, board2, i + di[k], j + dj[k], row, col);
}
}
}
void bfs(vector<vector<char> >& board, vector<vector<char> >& board2,
int i, int j, int& row, int& col) {
queue<pair<int, int> > levelPoints;
levelPoints.push(make_pair(i, j));
while (!levelPoints.empty()) {
int size = levelPoints.size();
for (int n = 0; n < size; ++n) {
i = levelPoints.front().first;
j = levelPoints.front().second;
levelPoints.pop();
board[i][j] = 'X'; board2[i][j] = 'O';
// search in four directions
int di[4] = {0, 0, -1, 1};
int dj[4] = {1, -1, 0, 0};
for (int k = 0; k < 4; ++k) {
if (i + di[k] >= 0 && i + di[k] < row
&& j + dj[k] >= 0 && j + dj[k] < col
&& board[i + di[k]][j + dj[k]] == 'O') {
levelPoints.push(make_pair(i + di[k], j + dj[k]));
}
}
}
}
}
};
int main() {
vector<vector<char> > board(5, vector<char>(5, 'X'));
board[0][0] = board[0][3] = board[1][1] = board[1][2] = 'O';
board[1][4] = board[2][1] = board[2][3] = board[3][0] = 'O';
board[3][2] = board[3][3] = board[3][4] = board[4][2] = 'O';
board[4][4] = 'O';
Solution s;
s.solve(board);
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
cout << board[i][j];
}
cout << endl;
}
return 0;
}