[LeetCode] 711. Number of Distinct Islands II_hard tag: DFS [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) 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.

这个题目实际上就是[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

 1 class Solution:
 2     def distintIslands(self, grid):
 3         if not grid or len(grid[0]) == 0: return 0
 4         self.ans = 0
 5         def addIsland(shape, shapes):
 6             xs, ys = zip(*shape)
 7             Xmin, Ymin = min(xs), min(ys)
 8             island1 = tuple(sorted((x-Xmin, y-Ymin) for x, y in shape))# original local 位置
 9             if island1 in shapes: return
10             xs, ys = zip(*island1)  # Xmax, Ymax should be max in island1, not shape
11             Xmax, Ymax = max(xs), max(ys)
12             island2 = tuple(sorted((-x+Xmax, y) for x, y in island1)) #x axis mirrow
13             island3 = tuple(sorted((x, -y + Ymax) for x, y in island1)) # y axis mirror
14             island4 = tuple(sorted((-x + Xmax, -y + Ymax) for x, y in island1)) # origin mirror
15 
16             island5 = tuple(sorted((y, x) for x, y in island1)) # diagonal mirror
17             island6 = tuple(sorted((-x+ Ymax, y) for x, y in island5))# note island5, not island1
18             island7 = tuple(sorted((x, -y + Xmax) for x, y in island5))
19             island8 = tuple(sorted((-x+ Ymax, -y + Xmax) for x, y in island5))
20             self.ans += 1    
21             shapes |= set([island1, island2,island3, island4,island5, island6,island7, island8])
22         def dfs(grid, i, j , shape, visited):
23             if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == 1 and (i, j) not in visited:
24                 visited.add((i,j))
25                 shape.append((i,j))
26                 dfs(grid, i-1, j, shape, visited)
27                 dfs(grid, i+1, j, shape, visited)
28                 dfs(grid, i, j-1, shape, visited)
29                 dfs(grid, i, j+1, shape, visited)
30         lr, lc, shapes, visited = len(grid), len(grid[0]), set(), set()
31         for i in range(lr):
32             for j in range(lc):
33                 if grid[i][j] == 1 and (i, j) not in visited:
34                     shape = []
35                     dfs(grid, i, j, shape, visited)
36                     addIsland(shape, shapes)
37         return self.ans

4. test cases

the test cases in the beginning.

猜你喜欢

转载自www.cnblogs.com/Johnsonxiong/p/9297525.html
今日推荐