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:

11000
10000
00001
00011

Given the above grid map, return 1.

Notice that:

11
1

and

 1
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:

11100
10001
01001
01110

Given the above grid map, return 2.

Here are the two distinct islands:

111
1

and

1
1

Notice that:

111
1

and

1
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进行去重。

好题。

#include "header.h"
#include <unordered_set>
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT1D(x) do {REP(i,(x).size()) cout << x[i] << " "; cout << endl;} while 0; #define PRINT1D(X) \
{ \
REP(i, (X).size()){ \
cout << (X)[i] << " "; \
} \
cout << endl; \
} \ #define PRINT2D(X) \
{ \
REP(i, (X).size()){ \
REP(j,(X)[].size()){ \
cout << (X)[i][j] << " "; \
} \
cout << endl; \
} \
cout << endl; \
} \ class Solution {
private:
unordered_map<int, vector<pair<int,int> > > map;
public:
void dfs(vector<vector<int>>& grid, int r, int c, int cnt){
grid[r][c] = ;
map[cnt].push_back({r,c});
int n = grid.size();
int m = grid[].size();
if(r < n && r >= && c < m && c >= && grid[r][c] == ){
dfs(grid, r+, c, cnt);
dfs(grid, r, c+, cnt);
dfs(grid, r-, c, cnt);
dfs(grid, r, c-, cnt);
}
} vector<pair<int,int>> norm(vector<pair<int,int>> originalshape){
vector<vector<pair<int,int>>> s();
//sort(ALL(originalshape));
for(auto p : originalshape){
int x = p.first, y = p.second;
s[].push_back({x,y});
s[].push_back({x,-y});
s[].push_back({-x,-y});
s[].push_back({-x,y});
s[].push_back({y,-x});
s[].push_back({-y,-x});
s[].push_back({-x,-y});
s[].push_back({y,x});
}
//sort(ALL(s));
for(auto &l : s ) sort(ALL(l));
for(auto &l : s){
auto l1st = l[];
REP(i,l.size()){
l[i].first = l[i].first - l1st.first;
l[i].second = l[i].second - l1st.second;
}
l1st.first = ;
l1st.second = ;
}
sort(ALL(s));
return s[];
}; int numDistinctIslands2(vector<vector<int>>& grid) {
set<vector<pair<int,int>>> s;
int cnt = ;
REP(i,grid.size()){
REP(j,grid[].size()){
if(grid[i][j] == ){
dfs(grid, i, j, ++cnt);
s.insert(norm(map[cnt]));
}
}
}
return s.size();
}
}; int main() {
vector<vector<int>> mtx(,vector<int>(,));
set<vector<int>>s;
Solution s1 = Solution();
s1.numDistinctIslands2(mtx);
}

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. Phoenix批量提交优化,官网的demo

    1 Phoenix的批量insert官网代码,最佳实践 try (Connection conn = DriverManager.getConnection(url)) { conn.setAutoC ...

  2. 2019-2020-1 20199319《Linux内核原理与分析》第一周作业

    一.Linux系统简介 通过实验一了解了Linux 的历史,Linux与windows之间的区别以及学习Linux的方法.因为一直用的都是windows系统,习惯了图形界面,而Linux是通过输入命令 ...

  3. Linux配置JDK环境

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  4. 8.CNN应用于手写字识别

    import numpy as np from keras.datasets import mnist from keras.utils import np_utils from keras.mode ...

  5. TF启程

    我第一次开始接触到TensorFlow大概是去年五月份,大三下,如果一年多已过,我却还在写启程..这进度,实在汗颜.. 一个完整的tensorflow程序可以分为以下几部分: Inputs and P ...

  6. maven的配置和eclipse maven插件安装

    1.下载maven:http://maven.apache.org/download.cgi 2. 配置环境变量: 3. 修改maven文件夹下bin/conf/settings.xml:maven仓 ...

  7. 关于GRPC的讲解

    gRPC服务发现&负载均衡 https://segmentfault.com/a/1190000008672912?utm_source=tag-newest GRPC编程指南 gRPC 介绍 ...

  8. python和ruby:一些需要注意的小区别。

    python和ruby的一些区别 基础区别 运算符号/和// ruby只有/符号.它根据操作的数字类型返回对应的结果.如果数字的类型是int,则返回整除结构,如果是float,则返回float类型的计 ...

  9. c# 获取api 数据

    private string GetDataFromServerApi(string url, string body) { string str = ""; try { Http ...

  10. 启动SpringBoot web项目出现 Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3,....

    详细错误信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> ...