Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

Count the number of distinct islands. An island is considered to be the same as another if they have the same shape, or have the same shape after rotation (90, 180, or 270 degrees only) or reflection (left/right direction or up/down direction).

Example 1:

  1. 11000
  2. 10000
  3. 00001
  4. 00011

Given the above grid map, return 1.

Notice that:

  1. 11
  2. 1

and

  1. 1
  2. 11

are considered same island shapes. Because if we make a 180 degrees clockwise rotation on the first island, then two islands will have the same shapes.

Example 2:

  1. 11100
  2. 10001
  3. 01001
  4. 01110

Given the above grid map, return 2.

Here are the two distinct islands:

  1. 111
  2. 1

and

  1. 1
  2. 1

Notice that:

  1. 111
  2. 1

and

  1. 1
  2. 111

are considered same island shapes. Because if we flip the first array in the up/down direction, then they have the same shapes.

Note: The length of each dimension in the given grid does not exceed 50.

这道题的难度比较大,涉及到的细节也很多,是一道很好的题。

注意几个细节:

1. set 由红黑树实现,unordered_set由哈希表实现,所以想要给vector去重,要用set。

2. for(auto l : s) ... 这个时候l是一个value,而for(auto &l : s) ... 这个时候l是一个reference。

3. 上下左右翻转,旋转90,180,270,对于一个(x,y)来讲正好对应8种情况。((+/-)(x/y),(+/-)()),可以自己推一遍。

4. 在norm函数里,有两次排序,这两次排序至关重要,第一次是对每一个翻转情况中的点进行排序,这一次排序的目的是为了让

两个ISLAND对应的翻转中最小的点排到第一个来,这样做的目的是因为只有这样,之后两个ISLAND以该点作为原点进行计算时,

两个ISLAND才能得到一样的点的序列。

第二次排序是为了在所有可能中拿点序最小的一个,拿最大也可以,只要把这八个可能用一个来表示就行了,然后插入集合中,利用

set进行去重。

好题。

  1. #include "header.h"
  2. #include <unordered_set>
  3. #define ALL(x) (x).begin(), (x).end()
  4. #define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
  5. #define REP(i, n) FOR(i, 0, n)
  6. #define PRINT1D(x) do {REP(i,(x).size()) cout << x[i] << " "; cout << endl;} while 0;
  7.  
  8. #define PRINT1D(X) \
  9. { \
  10. REP(i, (X).size()){ \
  11. cout << (X)[i] << " "; \
  12. } \
  13. cout << endl; \
  14. } \
  15.  
  16. #define PRINT2D(X) \
  17. { \
  18. REP(i, (X).size()){ \
  19. REP(j,(X)[].size()){ \
  20. cout << (X)[i][j] << " "; \
  21. } \
  22. cout << endl; \
  23. } \
  24. cout << endl; \
  25. } \
  26.  
  27. class Solution {
  28. private:
  29. unordered_map<int, vector<pair<int,int> > > map;
  30. public:
  31. void dfs(vector<vector<int>>& grid, int r, int c, int cnt){
  32. grid[r][c] = ;
  33. map[cnt].push_back({r,c});
  34. int n = grid.size();
  35. int m = grid[].size();
  36. if(r < n && r >= && c < m && c >= && grid[r][c] == ){
  37. dfs(grid, r+, c, cnt);
  38. dfs(grid, r, c+, cnt);
  39. dfs(grid, r-, c, cnt);
  40. dfs(grid, r, c-, cnt);
  41. }
  42. }
  43.  
  44. vector<pair<int,int>> norm(vector<pair<int,int>> originalshape){
  45. vector<vector<pair<int,int>>> s();
  46. //sort(ALL(originalshape));
  47. for(auto p : originalshape){
  48. int x = p.first, y = p.second;
  49. s[].push_back({x,y});
  50. s[].push_back({x,-y});
  51. s[].push_back({-x,-y});
  52. s[].push_back({-x,y});
  53. s[].push_back({y,-x});
  54. s[].push_back({-y,-x});
  55. s[].push_back({-x,-y});
  56. s[].push_back({y,x});
  57. }
  58. //sort(ALL(s));
  59. for(auto &l : s ) sort(ALL(l));
  60. for(auto &l : s){
  61. auto l1st = l[];
  62. REP(i,l.size()){
  63. l[i].first = l[i].first - l1st.first;
  64. l[i].second = l[i].second - l1st.second;
  65. }
  66. l1st.first = ;
  67. l1st.second = ;
  68. }
  69. sort(ALL(s));
  70. return s[];
  71. };
  72.  
  73. int numDistinctIslands2(vector<vector<int>>& grid) {
  74. set<vector<pair<int,int>>> s;
  75. int cnt = ;
  76. REP(i,grid.size()){
  77. REP(j,grid[].size()){
  78. if(grid[i][j] == ){
  79. dfs(grid, i, j, ++cnt);
  80. s.insert(norm(map[cnt]));
  81. }
  82. }
  83. }
  84. return s.size();
  85. }
  86. };
  87.  
  88. int main() {
  89. vector<vector<int>> mtx(,vector<int>(,));
  90. set<vector<int>>s;
  91. Solution s1 = Solution();
  92. s1.numDistinctIslands2(mtx);
  93. }

LC 711. Number of Distinct Islands II的更多相关文章

  1. [LeetCode] 711. Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  2. [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  3. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. [LeetCode] 694. Number of Distinct Islands 不同岛屿的个数

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  6. 694. Number of Distinct Islands 形状不同的岛屿数量

    [抄题]: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land ...

  7. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  8. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. LeetCode - Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. 第九章、import 和from ...import

    目录 第九章.import 和from ...import 一.import和 from ...import ... 二.import模块名 第九章.import 和from ...import 一. ...

  2. 【转】awk学习笔记

    Awk学习笔记 整理:Jims of 肥肥世家 <jims.yang@gmail.com> Copyright © 2004 本文遵从GPL协议,欢迎转载.修改.散布. 第一次发布时间:2 ...

  3. Linux计划任务与压缩归档

    计划任务分为两种形式 第一种:定时性的:也就是例行,每隔一定的周期就要重复来做这个任务. 第二种:突发性的:临时决定,只执行一次的任务. 用到的命令有两个 at:它是一个可以处理仅执行一次的任务就结束 ...

  4. hdf5文件、tqdm模块、nunique、read_csv、sort_values、astype、fillna

    pandas.DataFrame.to_hdf(self, path_or_buf, key, **kwargs): Hierarchical Data Format (HDF) ,to add an ...

  5. easypoi 版本依赖关系

    <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactI ...

  6. java 日期增加

    import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public cl ...

  7. 【Eclipse】Macbook eclipse 指定JDK

    1. vi /Applications/eclipse/Eclipse.app/Contents/Eclipse/eclipse.ini 输入 -vm /Library/Java/JavaVirtua ...

  8. Malloc Maleficarum复盘

    1.hos复盘 hos即伪造堆块,free栈上地址,然后下一个malloc去分配一个fastbin(栈上),包含返回地址. 代码来源 他这个我直接复现有问题,咨询了joker师傅,应该是gcc版本问题 ...

  9. web文件夹上传源码

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  10. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...