-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution694.java
More file actions
executable file
·30 lines (27 loc) · 1.21 KB
/
Copy pathSolution694.java
File metadata and controls
executable file
·30 lines (27 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
class Solution694 {
private Set<List<Integer>> islands = new HashSet<>();
public int numDistinctIslands(int[][] grid) {
boolean[][] visited = new boolean[grid.length][grid[0].length];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1 && !visited[i][j]) {
List<Integer> path = new LinkedList<>();
dfs(grid, visited, i, j, new int[] {i, j}, path);
islands.add(path);
}
}
}
return islands.size();
}
public void dfs(int[][] grid, boolean[][] visited, int x, int y, int[] direction, List<Integer> path) {
if (x < 0 || x >= grid.length || y < 0 || y >= grid[0].length || grid[x][y] != 1 || visited[x][y]) return;
visited[x][y] = true; // 标记当前结点已经访问
path.add(x - direction[0]); // 记录访问路径
path.add(y - direction[1]);
dfs(grid, visited, x + 1, y, direction, path);
dfs(grid, visited, x, y + 1, direction, path);
dfs(grid, visited, x - 1, y, direction, path);
dfs(grid, visited, x, y - 1, direction, path);
}
}