-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiggestIsland.java
More file actions
167 lines (153 loc) · 5.05 KB
/
BiggestIsland.java
File metadata and controls
167 lines (153 loc) · 5.05 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Perfomrs search of the biggest "island" in a matrix.
*/
package matrix;
import java.util.ArrayList;
import java.util.Arrays;
//Class to implement the search for biggest island.
public class BiggestIsland {
/**
* Sets an the matrix, runs the algorithm and prints the result.
* @param args Array of arguments for launching the program. Ignored.
* @export
*/
public static void main(String[] args) {
int[][] matrix = {
{0, 1, 1 , 1},
{1, 0, 1 , 1},
{1, 1, 0 , 1},
{1, 0, 0 , 1}
};
MatrixMap.print(matrix);
System.out.println(countBiggestIsland1(matrix));
System.out.println(countBiggestIsland2(matrix));
}
/**
* Counts the number points in the biggest island.
* @param ocean The initial matrix to search in.
* @return The number of points in the biggest island.
*/
public static int countBiggestIsland2(int[][] ocean) {
int len = ocean.length, maxSize = 0;
int[][] visited = new int[len][len];
for (int x = 0; x < len; x++) {
for (int y = 0; y < len; y++) {
int size = countIsland(x, y, ocean, visited);
if (size > maxSize) {
maxSize = size;
}
}
}
return maxSize;
}
/**
* Gets the number of points in an island at x, y.
* @param x The x coordinate in an ocean.
* @param y The y coordinate in an ocean.
* @param ocean The initial matrix to search in.
* @param visited the matrix of same size as island, 1 if the point
* was previously analysed, 0 if not.
* @return The number of points in the biggest island.
*/
public static int countIsland(int x, int y, int[][] ocean, int[][] visited) {
if (x < 0 || y < 0 || x >= ocean.length || y >= ocean.length) {
return 0;
}
if (visited[x][y] == 1) {
return 0;
}
if (ocean[x][y] == 0) {
return 0;
}
visited[x][y] = 1;
return 1 + countIsland(x + 1, y, ocean, visited) +
countIsland(x - 1, y, ocean, visited) +
countIsland(x, y + 1, ocean, visited) +
countIsland(x, y - 1, ocean, visited);
}
/**
* Counts the number points in the biggest island.
* @param ocean The initial matrix to search in.
* @return The number of points in the biggest island.
*/
public static int countBiggestIsland1(int[][] ocean) {
//Getting each point in a seperate island.
ArrayList<ArrayList<Point>> islands = new ArrayList<ArrayList<Point>>();
for (int x = 0, len = ocean.length; x < len; x++) {
for (int y = 0; y < len; y++) {
if (ocean[x][y] == 1) {
ArrayList<Point> island = new ArrayList<Point>();
island.add(new Point(x, y));
islands.add(island);
}
}
}
if (islands.size() == 0) {
return 0;
}
//If islands have adjusent points, merge them together.
int currentIsland = 0;
ArrayList<Integer> mergedIslands = new ArrayList<Integer>();
while (currentIsland >= 0) {
currentIsland = -1;
for (int i = 0, islandsNum = islands.size(); i < islandsNum; i++) {
for (int j = 0, pointsNum = islands.get(i).size(); j < pointsNum; j++) {
Point p = islands.get(i).get(j);
mergedIslands = getAdjustmentIsland(p, i, islands);
if (mergedIslands.size() > 0) {
currentIsland = i;
break;
}
}
if (currentIsland >= 0) {
break;
}
}
for (int num : mergedIslands) {
for (int j = 0, pointsNum = islands.get(num).size(); j < pointsNum; j++) {
Point p = islands.get(num).get(j);
islands.get(currentIsland).add(p);
}
}
ArrayList<ArrayList<Point>> newIslands = new ArrayList<ArrayList<Point>>();
for (int i = 0, islandsNum = islands.size(); i < islandsNum; i++) {
if (mergedIslands.indexOf(i) < 0) {
newIslands.add(islands.get(i));
}
}
islands = newIslands;
}
//Get the maximum island size by iterating through the islands.
int max = 1;
for (ArrayList<Point> island : islands) {
if (island.size() > max) {
max = island.size();
}
}
return max;
}
/**
* Given the island, and the point searches for any adjusent other island.
* @param p The point in the island to search from.
* @param exIsland The index of the island, p came from.
* @param islands The array of all islands currently available.
* @return Array of indexes of the adjusent islands.
*/
static ArrayList<Integer> getAdjustmentIsland(Point p, int exIsland, ArrayList<ArrayList<Point>> islands) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i = 0, islandsNum = islands.size(); i < islandsNum; i++) {
if (i == exIsland) {
continue;
}
for (int j = 0, pointsNum = islands.get(i).size(); j < pointsNum; j++) {
Point curP = islands.get(i).get(j);
if ((curP.y == p.y && ((curP.x == p.x + 1) || (curP.x == p.x - 1))) ||
(curP.x == p.x && ((curP.y == p.y + 1) || (curP.y == p.y - 1)))) {
result.add(i);
break;
}
}
}
return result;
}
}