|
8 | 8 |
|
9 | 9 | public class _694 {
|
10 | 10 | public static class Solution1 {
|
11 |
| - /** |
12 |
| - * My original idea: |
13 |
| - * my not fully working yet: the equals() and hashcode() methods need to be refined |
14 |
| - * because HashSet is not really filtering the islands wiht the same shape. |
15 |
| - */ |
16 |
| - class Quadrilateral { |
17 |
| - int[] topLeft; |
18 |
| - int[] bottomLeft; |
19 |
| - int[] topRight; |
20 |
| - int[] bottomRight; |
21 |
| - int area; |
22 |
| - |
23 |
| - public Quadrilateral(int i, int j) { |
24 |
| - this.area = 0; |
25 |
| - this.topLeft = new int[]{i, j}; |
26 |
| - this.topRight = new int[]{i, j}; |
27 |
| - this.bottomLeft = new int[]{i, j}; |
28 |
| - this.bottomRight = new int[]{i, j}; |
29 |
| - } |
30 |
| - |
31 |
| - @Override |
32 |
| - public boolean equals(Object o) { |
33 |
| - if (this == o) { |
34 |
| - return true; |
35 |
| - } |
36 |
| - if (!(o instanceof Quadrilateral)) { |
37 |
| - return false; |
38 |
| - } |
39 |
| - |
40 |
| - Quadrilateral that = (Quadrilateral) o; |
41 |
| - return this.area == that.area && checkDistance(that); |
42 |
| - } |
43 |
| - |
44 |
| - private boolean checkDistance(Quadrilateral that) { |
45 |
| - int thisTop = computeDistance(this.topLeft, this.topRight); |
46 |
| - int thatTop = computeDistance(that.topLeft, that.topRight); |
47 |
| - if (thisTop != thatTop) { |
48 |
| - return false; |
49 |
| - } |
50 |
| - int thisRight = computeDistance(this.topRight, this.bottomRight); |
51 |
| - int thatRight = computeDistance(that.topRight, that.bottomRight); |
52 |
| - if (thisRight != thatRight) { |
53 |
| - return false; |
54 |
| - } |
55 |
| - int thisBottom = computeDistance(this.bottomRight, this.bottomLeft); |
56 |
| - int thatBottom = computeDistance(that.bottomRight, that.bottomLeft); |
57 |
| - if (thisBottom != thatBottom) { |
58 |
| - return false; |
59 |
| - } |
60 |
| - int thisLeft = computeDistance(this.bottomLeft, this.topLeft); |
61 |
| - int thatLeft = computeDistance(that.bottomLeft, that.topLeft); |
62 |
| - return thisLeft == thatLeft; |
63 |
| - } |
64 |
| - |
65 |
| - private int computeDistance(int[] A, int[] B) { |
66 |
| - return (int) (Math.pow(A[0] - B[0], 2) + Math.pow(A[1] - B[1], 2)); |
67 |
| - } |
68 |
| - |
69 |
| - @Override |
70 |
| - public int hashCode() { |
71 |
| - return area + computeDistance(this.topLeft, this.topRight) + computeDistance(this.topRight, this.bottomRight) |
72 |
| - + computeDistance(this.bottomRight, this.bottomLeft) + computeDistance(this.bottomLeft, this.topLeft); |
73 |
| - } |
74 |
| - |
75 |
| - public void addPoint(int i, int j) { |
76 |
| - //todo: check wether this point (i,j) is in the range, if not, expand the range |
77 |
| - if (i == topRight[0]) { |
78 |
| - topRight[1] = Math.max(topRight[1], j); |
79 |
| - } |
80 |
| - if (j == topRight[1]) { |
81 |
| - topRight[0] = Math.min(topRight[1], i); |
82 |
| - } |
83 |
| - if (i == topLeft[0]) { |
84 |
| - topLeft[1] = Math.min(topLeft[1], j); |
85 |
| - } |
86 |
| - if (j == topLeft[1]) { |
87 |
| - topLeft[0] = Math.min(topLeft[0], i); |
88 |
| - } |
89 |
| - if (i == bottomLeft[0]) { |
90 |
| - bottomLeft[1] = Math.min(bottomLeft[1], j); |
91 |
| - } |
92 |
| - if (j == bottomLeft[1]) { |
93 |
| - bottomLeft[0] = Math.max(bottomLeft[0], i); |
94 |
| - } |
95 |
| - if (j == bottomRight[1]) { |
96 |
| - bottomRight[0] = Math.max(bottomRight[0], i); |
97 |
| - } |
98 |
| - if (i == bottomRight[0]) { |
99 |
| - bottomRight[1] = Math.max(bottomRight[1], j); |
100 |
| - } |
101 |
| - } |
102 |
| - |
103 |
| - public void addArea() { |
104 |
| - this.area++; |
105 |
| - } |
106 |
| - } |
107 |
| - |
108 |
| - public int numDistinctIslands(int[][] grid) { |
109 |
| - Set<Quadrilateral> set = new HashSet<>(); |
110 |
| - int m = grid.length; |
111 |
| - int n = grid[0].length; |
112 |
| - for (int i = 0; i < m; i++) { |
113 |
| - for (int j = 0; j < n; j++) { |
114 |
| - if (grid[i][j] == 1) { |
115 |
| - Quadrilateral quadrilateral = dfs(grid, i, j, m, n, new Quadrilateral(i, j)); |
116 |
| - set.add(quadrilateral); |
117 |
| - } |
118 |
| - } |
119 |
| - } |
120 |
| - return set.size(); |
121 |
| - } |
122 |
| - |
123 |
| - private Quadrilateral dfs(int[][] grid, int i, int j, int m, int n, Quadrilateral quadrilateral) { |
124 |
| - if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) { |
125 |
| - return quadrilateral; |
126 |
| - } |
127 |
| - grid[i][j] = 0; |
128 |
| - quadrilateral.addPoint(i, j); |
129 |
| - quadrilateral.addArea(); |
130 |
| - quadrilateral = dfs(grid, i + 1, j, m, n, quadrilateral); |
131 |
| - quadrilateral = dfs(grid, i - 1, j, m, n, quadrilateral); |
132 |
| - quadrilateral = dfs(grid, i, j + 1, m, n, quadrilateral); |
133 |
| - quadrilateral = dfs(grid, i, j - 1, m, n, quadrilateral); |
134 |
| - return quadrilateral; |
135 |
| - } |
136 |
| - } |
137 |
| - |
138 |
| - public static class Solution2 { |
139 | 11 | int[][] directions = new int[][]{
|
140 | 12 | {0, 1},
|
141 | 13 | {1, 0},
|
|
0 commit comments