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 ...
随机推荐
- linux,下载与安装
1.下载地址 1.网易开源镜像站 http://mirrors.163.com 2.centos 官方站 http://www.centos.org 2.虚拟机 VirtualBox ------- ...
- ZOJ 3804--解题报告
题目相关: 3804相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5336 宠物(minion)在N*M的矩形玩游戏 ...
- 143. Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- uestc1888 Birthday Party 组合数学,乘法原理
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=25539#problem/G 题目意思: 有n个人,每个人有一个礼物,每个人能拿 ...
- apache开启url rewrite模块
在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...
- php配置伪静态的方法
mod_rewrite是Apache的一个非常强大的功能,它可以实现伪静态页面.下面我详细说说它的使用方法 .检测Apache是否支持mod_rewrite 通过php提供的phpinfo()函数查看 ...
- hdu3416 最短路+最大流
题意:有 n 点 m 边,有出发点 A 到达点 B ,只允许走原图中的最短路,但每条边只允许被走一次,问最多能找出多少条边不重复的最短路 一开始做到的时候瞎做了一发最短路,WA了之后也知道显然不对,就 ...
- python study - 正则表达式
第 7 章 正则表达式 7.1. 概览 7.2. 个案研究:街道地址 7.3. 个案研究:罗马字母 7.3.1. 校验千位数 7.3.2. 校验百位数 7.4. 使用 {n,m} 语法 7.4.1. ...
- Subversion服务器搭建
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...
- 让图片完全显示出来,应对overflow,以及在背景中完全显示出来
1.应对overflow <script type="text/javascript"> $('.foo img').css('width','100%');//修正, ...