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"的更多相关文章

  1. [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 ...

  2. [LintCode] Number of Islands(岛屿个数)

    描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...

  3. [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 ...

  4. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  5. [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 ...

  6. 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 ...

  7. 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  ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. DISCOVAR de novo

    海宝建议用这个拼接软件 http://www.broadinstitute.org/software/discovar/blog/?page_id=98 DISCOVAR – variant call ...

  2. Oracle 12C RAC的optimizer_adaptive_features造成数据插入超时

    问题分析 使用10046事件追踪方式,直接生成上传时的数据库事件日志进行分析,发现主要区别在于以下两条sql语句在每次长时间上传时都有出现,并且执行用户不是上传用户,而是数据库SYS用户. ***** ...

  3. gpg --verify之"Can't check signature: No public key"

    自从XcodeGhost之后下载软件之后也会先验证一下md5sum,现在发现后面还有gpg签名,于是也开始学习一下. gpg的文件在centos6.4上是默认安装的,其安装使用可以参照ruanyife ...

  4. 多进程和atexit清理函数

    前言: 最近帮朋友review其模块服务代码, 使用的是python的twisted网络框架. 鉴于之前并没有使用过, 于是决定好好研究一番. 不过这个问题, 和twisted网络框架本身没有关系, ...

  5. crontab 安装 和一些 简单的命令

    crontab命 令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供 之后读取和执行.通常,crontab ...

  6. ZOJ 1056 The Worm Turns

    原题链接 题目大意:贪吃蛇的简化版,给出一串操作命令,求蛇的最终状态是死是活. 解法:这条蛇一共20格的长度,所以用一个20个元素的队列表示,队列的每个元素是平面的坐标.每读入一条指令,判断其是否越界 ...

  7. abbyy cup a

    link: http://codeforces.com/contest/331/problem/A2 /* ID: zypz4571 LANG: C++ TASK: abby_a.cpp */ #in ...

  8. 第n小的质数

    总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个正整数n,求第n小的质数. 输入 一个不超过10000的正整数n. 输出 第n小的质数. 样例输入 10 样例输出 29 代碼 ...

  9. 1-4-2 Windows数据类型与重要数据结构

    主要内容:介绍Windows数据类型与重要数据结构 1.数据类型 在Windows系统中定义了Windows应用程序中包含种类繁多的数据类型, 部分如下: WORD 16位无符号整数 typedef ...

  10. Resource Manager

    Azure Resource Manager overview https://azure.microsoft.com/en-us/documentation/articles/resource-gr ...