LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah I know.
class Solution {
unordered_set<int> hs; // start from 1
int max_k;
public:
void dfs(vector<vector<int>> &mt, int x, int y, int f)
{
int n = mt.size(), m = mt[].size();
mt[y][x] = f;
if(x > && mt[y][x - ] != f && mt[y][x - ] != ) // left
{
mt[y][x - ] = f;
dfs(mt, x - , y, f);
}
if(x < m - && mt[y][x + ] != f && mt[y][x + ] != ) // right
{
mt[y][x + ] = f;
dfs(mt, x + , y, f);
}
if(y > && mt[y - ][x] != f && mt[y - ][x] != ) // up
{
mt[y - ][x] = f;
dfs(mt, x, y - , f);
}
if(y < n - && mt[y + ][x] != f && mt[y + ][x] != ) // down
{
mt[y + ][x] = f;
dfs(mt, x, y + , f);
}
}
void update(vector<vector<int>> &mt, Point &p)
{
int n = mt.size(), m = mt[].size(); int minf = INT_MAX;
vector<Point> to_union;
if(p.x > ) // left
{
if(mt[p.y][p.x - ] != )
{
minf = min(minf, mt[p.y][p.x - ]);
to_union.push_back(Point(p.x - , p.y));
}
}
if(p.x < m - ) // right
{
if(mt[p.y][p.x + ] != )
{
minf = min(minf, mt[p.y][p.x + ]);
to_union.push_back(Point(p.x + , p.y));
}
}
if(p.y > ) // update
{
if(mt[p.y - ][p.x] != )
{
minf = min(minf, mt[p.y - ][p.x]);
to_union.push_back(Point(p.x, p.y - ));
}
}
if(p.y < n - ) // down
{
if(mt[p.y + ][p.x] != )
{
minf = min(minf, mt[p.y + ][p.x]);
to_union.push_back(Point(p.x, p.y + ));
}
}
////
if(minf == INT_MAX)
{
int np = max_k ++;
mt[p.y][p.x] = np;
hs.insert(np);
}
else
{
mt[p.y][p.x] = minf;
for(auto &tp:to_union)
{
if(mt[tp.y][tp.x] != && mt[tp.y][tp.x] != minf)
{
int old = mt[tp.y][tp.x];
dfs(mt, tp.x, tp.y, minf);
hs.erase(old);
}
}
}
}
/**
* @param n an integer
* @param m an integer
* @param operators an array of point
* @return an integer array
*/
vector<int> numIslands2(int n, int m, vector<Point>& operators){
vector<vector<int>> mt(m, vector<int>(n));
max_k = ;
vector<int> ret;
for(auto &p : operators)
{
update(mt, p);
ret.push_back(hs.size());
}
return ret;
}
};
LintCode "Number of Islands II"的更多相关文章
- [LeetCode] Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...
- [LeetCode] 305. Number of Islands II 岛屿的数量之二
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- [LeetCode] Number of Islands II
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may pe ...
- Leetcode: Number of Islands II && Summary of Union Find
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- 305. Number of Islands II
题目: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand ...
- [Swift]LeetCode305. 岛屿的个数 II $ Number of Islands II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- LeetCode – Number of Islands II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
随机推荐
- Qt之QCheckBox
简述 QCheckBox继承自QAbstractButton,它提供了一个带文本标签的复选框. QCheckBox(复选框)和QRadioButton(单选框)都是选项按钮.这是因为它们都可以在开(选 ...
- Prepared Java infrastructure for distributed scenarios
code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...
- 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- 单元测试--四则运算2程序(c++)
源代码: //2016 3.6 Cheng Qiqin //四则运算改进 #include <iostream> #include<ctime> #include<cst ...
- hdu4642 Fliping game ——博弈
link:http://acm.hdu.edu.cn/showproblem.php?pid=4642 refer to: http://www.cnblogs.com/jackge/archive/ ...
- 百度APIStore
链接 http://apistore.baidu.com/ 提供了许多免费的api接口,用来做功能性的查询
- GridView用法大全(转)
http://www.cnblogs.com/sufei/archive/2010/03/27/1698590.html
- 选数 2002年NOIP全国联赛普及组
题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整 ...
- 铺地毯 2011年NOIP全国联赛提高组
题目描述 Description 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有n 张地毯,编号从1 到n.现在将这些地毯按照编号从 ...
- 2016 Hunan Province Programming Contest
2016 Hunan Province Programming Contest A. 2016 题意 \(1 \le a \le n, 1 \le b \le m\) ,其中\(1 \le n,m \ ...