130. Surrounded Regions 将包围的符号变换 BFS & DFS & UNION find
来源:互联网 发布:mmd成人动作数据 编辑:程序博客网 时间:2024/06/10 05: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 XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX 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'; } }};
0 0
- 130. Surrounded Regions 将包围的符号变换 BFS & DFS & UNION find
- BFS、DFS——surrounded-regions矩阵包围XXOO
- 【DFS||BFS】Surrounded Regions
- Surrounded Regions 被包围的区域
- 被包围的棋子 Surrounded Regions
- LeetCode | Surrounded Regions(包围的区域)
- LeetCode 130. Surrounded Regions(包围区域)
- leetcode:Union Find:Surrounded Regions(130)
- [leetcode] 130 surrounded regions dfs bfs
- 【LeetCode】 Surrounded Regions (BFS && DFS)
- leetCode-Surrounded Regions DFS与BFS解法
- 130. Surrounded Regions(BFS)
- (M)BFS:130. Surrounded Regions
- [LeetCode]-Surrounded Regions 找被包围的点
- Surrounded Regions ,BFS
- LeetCode[BFS]: Surrounded Regions
- 130:Surrounded Regions【BFS】
- Surrounded Regions BFS遍历
- leetcode No138. Copy List with Random Pointer
- File文件的 基本操作
- 基于N源码的ContentService数据通知机制
- Maven中Pom文件打包配置build
- Java基础复习---面向对象程序设计
- 130. Surrounded Regions 将包围的符号变换 BFS & DFS & UNION find
- Median of Two Sorted Arrays
- 51单片机串口通信(字符串接收和发送)
- linux c++ epoll的简单实现
- JavaScript常用的事件及简单应用
- Leetcode 112 Path Sum
- (新人)公司考核对web项目的一些要求 下次我要注意
- 将网页内容写入本地
- Centos7安装Golang1.6,并配置vim环境