LC 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) 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 用了第一种方式, ...
- [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 ...
- 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 ...
随机推荐
- postman安装时提示打不开
安装postman6.6.1时,提示打不开,如下图: 解决办法: 1.找到以下两个路径直接删除文件,注安装路径不同有可能不同 C:\Users\Administrator\AppData\Roamin ...
- (备忘)openssl的证书格式转换
PKCS 全称是Public-KeyCryptography Standards ,是由 RSA 实验室与其它安全系统开发商为促进公钥密码的发展而制订的一系列标准,PKCS 目前共发布过15 个标准. ...
- 构建虚拟工控环境系列 - 西门子虚拟PLC
一. 概述 跟随着工控安全一路走来,工控安全市场今年明显有相当大的改善,无论从政策还是客户需求,都在逐步扩大中.但是,搞工控安全研究的人员却寥寥无几.一方面工控安全是个跨学课的技术,需要了解多方面的知 ...
- 静态static最基础的知识
static静态: 常见修饰的内容: 1.变量: 修饰变量时,叫静态变量或类变量.此变量为类所有随着虚拟机加载类是而加载入方法区,此静态变量为该类所有对象共享,在内存中只有一个副本,它 当且仅当 类的 ...
- idea管理tomcat
第一步,打开idea的文件——>设置——选择Application Servers: 第二步,点击+号,下拉选择Tomcat Server: 如果已经配置了环境变量CATALINA_HOME,也 ...
- 合并K个sorted list
合并k个已经排好序的数列是面试中也比较容易被问到的一个算法,有很多种解决,其中第一时间比较容易想到的解法如下: 对于这三组从小到大的数列: 如此循环,最终就将三个已经排序的数列的数字按从小到大的顺序排 ...
- 【NOIP2012】同余方程
原题: 求关于xx的同余方程ax≡1(mod b)的最小正整数解. 裸题 当年被这题劝退,现在老子终于学会exgcd了哈哈哈哈哈哈哈哈 ax≡1(mod b) => ax=1+by => ...
- Springboot定时任务实现动态配置Cron参数(从外部数据库获取)
https://blog.csdn.net/qq_35992900/article/details/80429245 我们主要讲解它的动态配置使用方法. 在刚开始使用的时候,我们更改一个任务的执行时间 ...
- 利用jQuery实现图片无限循环轮播(不借助于轮播插件)
原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...
- HDFS知识点
1.通过代码验证集群的配置文件的优先级 HDFS文件上传 1.编写源代码 @Test public void testCopyFromLocalFile() throws IOException, I ...