130. Surrounded Regions 將包圍的符號變換 BFS & DFS & UNION find
阿新 • • 發佈:2019-02-11
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
1.DFS
class Solution { public: //1.用深度搜索的方法 void mark(vector<vector<char>>& board, int i, int j, int m, int n){ if(i > 1 && board[i-1][j] == 'O'){ board[i-1][j] = '1'; mark(board, i-1, j, m, n); } if(i+1 < m && board[i+1][j] == 'O'){ board[i+1][j] = '1'; mark(board, i+1, j, m, n); } if(j > 1 && board[i][j-1] == 'O'){ board[i][j-1] = '1'; mark(board, i, j-1, m, n); } if(j+1 < n && board[i][j+1] == 'O'){ board[i][j+1] = '1'; mark(board, i, j+1, m, n); } } void solve(vector<vector<char>>& board) { int m = board.size(); if(m == 0) return; int n = board[0].size(); if(n == 0) return; for(int i = 0; i < n; i++){ //對頭尾兩行進行邊緣‘O’的查詢 if(board[0][i] == 'O'){ board[0][i] = '1'; mark(board, 0, i, m, n); } if(m > 1){ if(board[m-1][i] == 'O'){ board[m-1][i] = '1'; mark(board, m-1, i, m, n); } } } for(int j = 0; j < m; j++){//對頭尾兩列進行邊緣‘O’的查詢 if(board[j][0] == 'O'){ board[j][0] = '1'; mark(board, j, 0, m, n); } if(n > 1){ if(board[j][n-1] == 'O'){ board[j][n-1] = '1'; mark(board, j, n-1, m, n); } } } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == 'O') board[i][j] = 'X'; } } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == '1') board[i][j] = 'O'; } } } };
2.BFS
class Solution { public: void bfs(vector<vector<char>>& board, int x, int y, int m, int n){ queue<pair<int, int>>q; q.push(make_pair(x,y)); board[x][y] = '+'; while(!q.empty()){ pair<int, int>cur = q.front(); q.pop(); pair<int, int>pos[4] = {{cur.first-1, cur.second}, {cur.first+1, cur.second},{cur.first, cur.second+1}, {cur.first, cur.second-1}}; for(int i = 0;i < 4; i++){ int xx = pos[i].first; int yy = pos[i].second; if(xx >= 0 && xx < m && yy >= 0 && yy < n && board[xx][yy] == 'O'){ q.push(pos[i]); board[xx][yy] = '+'; } } } } void solve(vector<vector<char>>& board) { //BFS int m = board.size(); if(m == 0) return; int n = board[0].size(); if(n == 0) return; for(int i = 0; i < n; i++){ if(board[0][i] == 'O') bfs(board, 0, i, m, n); if(board[m-1][i] == 'O') bfs(board, m-1, i, m, n); } for(int i = 0; i < m; i++){ if(board[i][0] == 'O') bfs(board, i, 0, m, n); if(board[i][n-1] == 'O') bfs(board, i, n-1, m, n); } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == 'O') board[i][j] = 'X'; } } for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(board[i][j] == '+') board[i][j] = 'O'; } } } };
3.union find
別人的思想;
並查集常用來解決連通性的問題,即將一個圖中連通的部分劃分出來。當我們判斷圖中兩個點之間是否存在路徑時,就可以根據判斷他們是否在一個連通區域。
當資料較多時,並查集比BFS和DFS演算法適用。
但是,並查集的缺陷就是不能夠求出連通的兩個點之間的路徑,但是DFS演算法或者BFS演算法適用。
並查集的思想就是,同一個連通區域內的所有點的根節點是同一個。將每個點對映成一個數字。先假設每個點的根節點就是他們自己,然後我們以此輸入連通的點對,然後將其中一個點的根節點賦成另一個節點的根節點,這樣這兩個點所在連通區域又相互連通了。
並查集的主要操作有:
- find(int m):這是並查集的基本操作,查詢m的根節點。
- checkConnection(int m,int n):判斷m,n兩個點是否在一個連通區域。
- Union(int m,int n):合併m,n兩個點所在的連通區域。
當然,並查集還有較多優化,比如怎麼避免並查集的樹退化成一條鏈。合併的時候,應該儘量讓小樹根節點連在大樹根節點上,儘量使得整個樹扁平。
程式碼:class Solution {
//union find主要找根節點,union 主要對兩兩(右下)進行合併,同時若一個是邊緣的,則使另一個也為邊緣
private:
vector<int>root;
vector<bool>isedge;
public:
void unions(int x, int y){
int root_x = find(x);
int root_y = find(y);
if(root_x == root_y) return;
root[root_x] = root_y; //將root_x 作為root_y的子節點
if(isedge[root_x]) isedge[root_y] = true;
}
int find(int x){
while(root[x] != x){
root[x] = root[root[x]];
x = root[x];
}
return x;
}
void solve(vector<vector<char>>& board) {
if(board.size() == 0 || board[0].size() == 0) return;
int height = board.size(), width = board[0].size();
int n = height* width;
root = vector<int>(n, 0);
isedge = vector<bool>(n, false);
//1. 初始化root和isedge陣列
for(int i = 0; i < n; i++){
int x = i / width, y = i % width;
root[i] = i;
if((board[x][y] == 'O') && ((x == 0) || (x == height-1) || (y == 0) || (y == width-1))) isedge[i] = true;
}
//2.union
for(int i = 0; i < n; i++){
int x = i/width, y = i % width;
if((x < height -1) && (board[x][y] == board[x+1][y])) unions(i, i + width); //跟下面的進行合併
if((y < width -1) && (board[x][y] == board[x][y+1])) unions(i, i+1);
}
//3.find
for(int i = 0; i < n; i++){
int x = i/width, y = i%width;
if(board[x][y] == 'O' && !isedge[find(i)]) board[x][y] = 'X';
}
}
};