A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah I know.

  1. class Solution {
  2. unordered_set<int> hs; // start from 1
  3. int max_k;
  4. public:
  5. void dfs(vector<vector<int>> &mt, int x, int y, int f)
  6. {
  7. int n = mt.size(), m = mt[].size();
  8. mt[y][x] = f;
  9. if(x > && mt[y][x - ] != f && mt[y][x - ] != ) // left
  10. {
  11. mt[y][x - ] = f;
  12. dfs(mt, x - , y, f);
  13. }
  14. if(x < m - && mt[y][x + ] != f && mt[y][x + ] != ) // right
  15. {
  16. mt[y][x + ] = f;
  17. dfs(mt, x + , y, f);
  18. }
  19. if(y > && mt[y - ][x] != f && mt[y - ][x] != ) // up
  20. {
  21. mt[y - ][x] = f;
  22. dfs(mt, x, y - , f);
  23. }
  24. if(y < n - && mt[y + ][x] != f && mt[y + ][x] != ) // down
  25. {
  26. mt[y + ][x] = f;
  27. dfs(mt, x, y + , f);
  28. }
  29. }
  30. void update(vector<vector<int>> &mt, Point &p)
  31. {
  32. int n = mt.size(), m = mt[].size();
  33.  
  34. int minf = INT_MAX;
  35. vector<Point> to_union;
  36. if(p.x > ) // left
  37. {
  38. if(mt[p.y][p.x - ] != )
  39. {
  40. minf = min(minf, mt[p.y][p.x - ]);
  41. to_union.push_back(Point(p.x - , p.y));
  42. }
  43. }
  44. if(p.x < m - ) // right
  45. {
  46. if(mt[p.y][p.x + ] != )
  47. {
  48. minf = min(minf, mt[p.y][p.x + ]);
  49. to_union.push_back(Point(p.x + , p.y));
  50. }
  51. }
  52. if(p.y > ) // update
  53. {
  54. if(mt[p.y - ][p.x] != )
  55. {
  56. minf = min(minf, mt[p.y - ][p.x]);
  57. to_union.push_back(Point(p.x, p.y - ));
  58. }
  59. }
  60. if(p.y < n - ) // down
  61. {
  62. if(mt[p.y + ][p.x] != )
  63. {
  64. minf = min(minf, mt[p.y + ][p.x]);
  65. to_union.push_back(Point(p.x, p.y + ));
  66. }
  67. }
  68. ////
  69. if(minf == INT_MAX)
  70. {
  71. int np = max_k ++;
  72. mt[p.y][p.x] = np;
  73. hs.insert(np);
  74. }
  75. else
  76. {
  77. mt[p.y][p.x] = minf;
  78. for(auto &tp:to_union)
  79. {
  80. if(mt[tp.y][tp.x] != && mt[tp.y][tp.x] != minf)
  81. {
  82. int old = mt[tp.y][tp.x];
  83. dfs(mt, tp.x, tp.y, minf);
  84. hs.erase(old);
  85. }
  86. }
  87. }
  88. }
  89. /**
  90. * @param n an integer
  91. * @param m an integer
  92. * @param operators an array of point
  93. * @return an integer array
  94. */
  95. vector<int> numIslands2(int n, int m, vector<Point>& operators){
  96. vector<vector<int>> mt(m, vector<int>(n));
  97. max_k = ;
  98. vector<int> ret;
  99. for(auto &p : operators)
  100. {
  101. update(mt, p);
  102. ret.push_back(hs.size());
  103. }
  104. return ret;
  105. }
  106. };

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. 博客Mac桌面编辑器-cnblogs

    Mac篇 公司的机器内存只有8G,不想再大动干戈为了Windows Live Writer装个Vmware了,谷歌娘讲MarsEdit不错,那就试试用这个写个试用贴呗   就是这货了,果然是火星来的, ...

  2. url截取判断(实现同级列表)

    <script> var dUrl=window.location.href; var cUrl=(dUrl.substring(0, dUrl.indexOf('list_'))); v ...

  3. hdu 5902 Seam Carving

    水题,直接上代码了 #include<cstdio> #include<cstring> #include<iostream> #include<cmath& ...

  4. CentOS下解决”用户账户is not in the sudoers file“问题

    如上图,在当前用户cent(我的用户名)下使用sudo命令时,提示"cent is not in the sudoers file. This incident will be report ...

  5. Sprint第二个冲刺(第七天)

    一.Sprint 计划会议: 现在简单的说下今天的会议情况:组员们除了完善之前做的功能,还打算实现把轮播图迁移到一个fragment中,方便管理.现在也准备着手实现商家上传商品的图片这个功能,虽说现在 ...

  6. 前后台Json的转换

    jsp:JSON.stringify(params):params:表示数组 servlet:Store store = (Store) JSONObject.toBean(JSONObject.fr ...

  7. tyvj1023 - 奶牛的锻炼 ——DP

    题目链接:https://www.tyvj.cn/Problem_Show.aspx?id=1023 #include <cstdio> #include <algorithm> ...

  8. HDU-2196 Computer (树形DP)

    题目大意:在一棵带边权的有根树中,对于每个点,找出它与离它最远的那个点的之间的距离. 题目分析:对于一个点,离它最远的点只有两种情况,一是它到叶子节点的最远距离,一是与它父亲的距离加上他的父亲到叶子节 ...

  9. 【NOI2015】软件包管理器

    NOI难得的水题,话说还是T2诶……又学到了线段树的一种新的魔性使用 看sxysxy大神的代码才写出来的,sxysxy_orz 原题: Linux用户和OSX用户一定对软件包管理器不会陌生.通过软件包 ...

  10. HTML5 拖放(Drag 和 Drop)

    拖放是一种常见的特性,即抓取对象以后拖到另一个位置. 在 HTML5 中,拖放是标准的一部分,任何元素都能够拖放. <!DOCTYPE HTML> <html> <hea ...