[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) 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.
Update 08/17/2018: 注意不能利用 [LeetCode] 694. Number of Distinct Islands里面的dfs function然后直接sort, 否则的话会有负数, 比如
0 1
1 1, 因此我们直接tuple(shape), 然后通过得到xMin, yMin, 然后去tuple.(这样可以避免负数的情况)
这个题目实际上就是[LeetCode] 694. Number of Distinct Islands 的变形, 我们实际上还是用那个题目的updated的做法, 用DFS去记录它的相对local 位置, 然后针对翻转和对称能够改变的加上original 位置, 总共有8 种情况, 如果original的每个坐标为(x,y), 那么其他的情况分别为(-x, y), (x,-y), (-x, -y), (y,x), (-y, x), (y, -x), (-y, -x), 只不过因为我们要的是相对位置, 那么每次在改变符号的时候要加上 Xmax or Ymax(注意的是变为local的坐标之后的max), 然后每次判断产生的local 位置在不在我们的shapes这个set里面, 如果不在的话, ans += 1, 并且将8种情况都加入到shapes 的set里面, 最后返回ans即可, 其实相对于694的题目, 只是加入了一个addIsland函数来判断是否可以翻转或者对称来跟已经有的local位置去进行比较.
1. Constraints
1) grid 可以empty, edge
2) max size will be [50*50]
3) each element will be 0 or 1
2. 思路
DFS, 因为中间有排序的过程, 所以我认为 T; O(m*n lg(m*n)) S; O(m*n)
3. Code
class Solution(object):
def numDistinctIslands2(self, grid):
if not grid or len(grid[0]) == 0 : return 0
lrc, visited, ans, shapes = [len(grid), len(grid[0])], set(), [0], set()
def dfs(i,j):
if 0<= i < lrc[0] and 0 <= j < lrc[1] and grid[i][j] and (i,j) not in visited:
visited.add((i,j))
shape.append((i, j))
dfs(i-1, j)
dfs(i+1, j)
dfs(i, j-1)
dfs(i, j+1)
def addShape(shape, shapes):
xs, ys = zip(*shape) # get xMin, yMin from original shape
xMin, yMin = min(xs), min(ys) # note: 必须通过这种方式来去得到local全部是正数的tuple
island0 = tuple(sorted((x-xMin, y - yMin) for x,y in shape))
if island0 in shapes: return
ans[0] += 1
xs, ys = zip(*island0) # get xMax, yMax from island0
xMax, yMax = max(xs) , max(ys)
island1 = tuple(sorted((-x + xMax, y ) for x, y in island0))
island2 = tuple(sorted((x, -y + yMax ) for x, y in island0))
island3 = tuple(sorted((-x + xMax, -y + yMax) for x, y in island0)) island4 = tuple(sorted((y, x) for x, y in island0))
island5 = tuple(sorted((-y + yMax, x) for x, y in island0))
island6 = tuple(sorted((y, -x + xMax) for x, y in island0))
island7 = tuple(sorted((-y + yMax, -x + xMax) for x, y in island0)) shapes |= set([island0, island1, island2, island3, island4, island5, island6, island7]) for i in range(lrc[0]):
for j in range(lrc[1]):
shape = []
dfs(i,j)
if shape:
addShape(shape, shapes)
return ans[0]
4. test cases
the test cases in the beginning.
[LeetCode] 711. Number of Distinct Islands II_hard tag: DFS的更多相关文章
- [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] 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]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] 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 ...
- 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) 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] 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 ...
- 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 用了第一种方式, ...
随机推荐
- Matlab PCA 算法
Matlab 自带PCA函数形式为 [mappedX, mapping] = pca(X, no_dims) 自己编写PCA函数的步骤 %第一步:输入样本矩阵%%%%%%%%%%%%%%%%%%%%% ...
- 【Spring Boot&& Spring Cloud系列】单点登录SSO概述
概念 单点登录(Singleton Sign On),简称为SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就能访问所有相互信任的应用系统. 也就 ...
- vue案例 - vue-awesome-swiper实现h5滑动翻页效果
说到h5的翻页,很定第一时间想到的是swiper.但是我当时想到的却是,vue里边怎么用swiper?! 中国有句古话叫:天塌下来有个高的顶着. 在前端圈里,总有前仆后继的仁人志士相继挥洒着热汗(这里 ...
- 解决:SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间提示问题
提示信息如下 “/”应用程序中的服务器错误. SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间. 问题现象: 问 ...
- 【CF888E】Maximum Subsequence 折半搜索
[CF888E]Maximum Subsequence 题意:给你一个序列{ai},让你从中选出一个子序列,使得序列和%m最大. n<=35,m<=10^9 题解:不小心瞟了一眼tag就一 ...
- 【BZOJ1210】[HNOI2004]邮递员 插头DP+高精度
[BZOJ1210][HNOI2004]邮递员 Description Smith在P市的邮政局工作,他每天的工作是从邮局出发,到自己所管辖的所有邮筒取信件,然后带回邮局.他所管辖的邮筒非常巧地排成了 ...
- 【C#】简单计算器源代码
form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...
- Unity3D 记第一次面试
事情是发生在2014-03-05 周三下午 在群里面看到上海艺游急聘Unity3D开发工程师,就整理了下简历投了去!直到接到电话通知我去面试才知道 我之前是有投了简历!太忙了 以至于真的忘了,不过那个 ...
- Windows Server 2008 R2之六活动目录域服务的卸载
活动目录域服务的卸载是将DC降级为独立服务器或成员服务器的过程. 在删除活动目录之前,为了防止操作失败操作系统故障,须对系统进行备份.同时,我们还必须对待删除的域控制器进行如下检查 1.是否有操作主控 ...
- yii---解决post请求出现500错误
在使用yii框架的时候,在发送数据请求的时候,POST请求会出现500错误,这是因为yii2开启了防御csrf的攻击机制,可去先去掉,在控制器里去掉:public $enableCsrfValidat ...