LeetCode 130. Surrounded Regions (并查集)
来源:互联网 发布:c语言界面设计 编辑:程序博客网 时间:2024/06/07 02:26
LeetCode 130. Surrounded Regions (并查集)
- LeetCode 130 Surrounded Regions 并查集
- 问题描述
- 解题思路
- 参考代码
Tags:
- Breadth-first Search
- Union Find
问题描述
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
Subscribe to see which companies asked this question.
解题思路
题意是把所有不与边界直接相连的O变为X。
使用并查集,将所有相连的O转换为并查集并且相连。
然后定义一个虚节点,表示边界。
把与边界相连的结点和虚节点链接,表示该节点与边界相连,不需要变成X。
最后把不与虚节点连接的O结点变为X。
参考代码
#include <iostream>#include <vector>using namespace std;class UF // union find{private: int *id; // parent link int *sz; // size of component for roots int count; // number of componentspublic: UF(int N) { count = N; id = new int[N]; sz = new int[N]; for (int i = 0; i < N; ++i) { id[i] = i; sz[i] = 1; } } ~UF() { delete[] id; delete[] sz; } int getCount() { return count; } int find(int i) { while (i != id[i]) { id[i] = id[id[i]]; // path compression by halving i = id[i]; } return i; } bool connected(int p, int q) // check if two union is connected { return find(p) == find(q); } void connect(int p, int q) // merge { int i = find(p); int j = find(q); if (i == j) return; // make smaller root point to larger one if(sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; } else { id[j] = i; sz[i] += sz[j]; } count--; }};class Solution{public: void solve(vector<vector<char>> &board) { int n = board.size(); if (n == 0)return; int m = board[0].size(); UF uf = UF(n * m + 1); // n * m is a dummy node, which means boundary for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { // if a 'O' node is on the boundary, connect it to the dummy node if((i == 0 || i == n - 1 || j == 0 || j == m - 1) && board[i][j] == 'O') { uf.connect(i * m + j, n * m); } // connect a 'O' node to its neighbor 'O' nodes else if (board[i][j] == 'O') { if (board[i - 1][j] == 'O') uf.connect(i * m + j, (i - 1) * m + j); if (board[i + 1][j] == 'O') uf.connect(i * m + j, (i + 1) * m + j); if (board[i][j - 1] == 'O') uf.connect(i * m + j, i * m + j - 1); if (board[i][j + 1] == 'O') uf.connect(i * m + j, i * m + j + 1); } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { // if a 'O' node is not connected to the dummy node, it is captured if (!uf.connected(i * m + j, n * m)) { board[i][j] = 'X'; } } } }};int main(){ vector<vector<char>> board = { {'X', 'X', 'X', 'X'}, {'X', 'O', 'O', 'X'} , {'X', 'X', 'O', 'X'}, {'X', 'O', 'X', 'X'} }; auto sl = new Solution(); sl->solve(board); for (auto a : board) { for (auto b : a) { cout << b << " "; } cout << endl; } system("pause"); return 0;}
0 0
- LeetCode 130. Surrounded Regions (并查集)
- (LeetCode 130)Surrounded Regions(并查集)
- LeetCode 130 Surrounded Regions(并查集)
- [LeetCode]130.Surrounded Regions
- LeetCode 130. Surrounded Regions
- [leetcode] 130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- leetcode 130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- [LeetCode]130. Surrounded Regions
- LeetCode 130. Surrounded Regions
- LeetCode *** 130. Surrounded Regions
- Leetcode 130. Surrounded Regions
- [leetcode] 130. Surrounded Regions
- LeetCode-130.Surrounded Regions
- Leetcode 130. Surrounded Regions
- [LeetCode] 130. Surrounded Regions
- [掌盟+]RecycleView实现"英雄资料"页和"发现"页
- 《产品经理方法论》
- STM32笔记0428/建立模板
- 关于Android源码的获取
- 4.29面试总结
- LeetCode 130. Surrounded Regions (并查集)
- POJ 3159 Candies(差分规划+SPFA)
- openvpn通过ldap或ad统一认证解决方案思路分享
- 学习淘淘商城第三十七课(中文分析器IK-Analyzer的使用)
- 第七章 类
- mysql--查看版本
- Effective Java系列读后感(三)-类和接口
- C++ 文件操作
- eclipse 常用快捷键整理