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 ...
随机推荐
- 博客Mac桌面编辑器-cnblogs
Mac篇 公司的机器内存只有8G,不想再大动干戈为了Windows Live Writer装个Vmware了,谷歌娘讲MarsEdit不错,那就试试用这个写个试用贴呗 就是这货了,果然是火星来的, ...
- url截取判断(实现同级列表)
<script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...
- hdu 5902 Seam Carving
水题,直接上代码了 #include<cstdio> #include<cstring> #include<iostream> #include<cmath& ...
- CentOS下解决”用户账户is not in the sudoers file“问题
如上图,在当前用户cent(我的用户名)下使用sudo命令时,提示"cent is not in the sudoers file. This incident will be report ...
- Sprint第二个冲刺(第七天)
一.Sprint 计划会议: 现在简单的说下今天的会议情况:组员们除了完善之前做的功能,还打算实现把轮播图迁移到一个fragment中,方便管理.现在也准备着手实现商家上传商品的图片这个功能,虽说现在 ...
- 前后台Json的转换
jsp:JSON.stringify(params):params:表示数组 servlet:Store store = (Store) JSONObject.toBean(JSONObject.fr ...
- tyvj1023 - 奶牛的锻炼 ——DP
题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1023 #include <cstdio> #include <algorithm> ...
- HDU-2196 Computer (树形DP)
题目大意:在一棵带边权的有根树中,对于每个点,找出它与离它最远的那个点的之间的距离. 题目分析:对于一个点,离它最远的点只有两种情况,一是它到叶子节点的最远距离,一是与它父亲的距离加上他的父亲到叶子节 ...
- 【NOI2015】软件包管理器
NOI难得的水题,话说还是T2诶……又学到了线段树的一种新的魔性使用 看sxysxy大神的代码才写出来的,sxysxy_orz 原题: Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包 ...
- HTML5 拖放(Drag 和 Drop)
拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. <!DOCTYPE HTML> <html> <hea ...