How to find the biggest island in the file
I have two-dimensional array of chars called map. Map contains three types of cell: x-land, u-river, o-sea. My task is to find the biggest island. Island is landmass connected in any of eight directions - up, top right, right... and surrounded by sea cells. There is a row that contains only sea tiles and can be interpreted as sealine that sea will not cross. Rivers do not exist on islands. I already made a method to find the longest river but i got stuck on finding the biggest isle, any advices?
the answear should be 19
public static int findLargestIsland(int x, int y) {
if (x < 0 x >= columnsAmount y < 0 y >= rowsAmount map[x][y] != 'x') { return 0; }
map[x][y] = '?';
int size = 1;
size += findLargestIsland(x - 1, y); size += findLargestIsland(x + 1, y); size += findLargestIsland(x, y - 1); size += findLargestIsland(x, y + 1); size += findLargestIsland(x - 1, y - 1); size += findLargestIsland(x - 1, y + 1); size += findLargestIsland(x + 1, y - 1); size += findLargestIsland(x + 1, y + 1);
return size; }
public static int findLargestIslandInMap() { int largestIsland = 0;
for (int i = 0; i < columnsAmount; i++) { for (int j = 0; j < rowsAmount; j++) { if (map[i][j] == 'x') { int currentIslandSize = findLargestIsland(i, j); largestIsland = Math.max(largestIsland, currentIslandSize); } } }
return largestIsland; }
if (x < 0 x >= columnsAmount y < 0 y >= rowsAmount map[x][y] != 'x') { return 0; }
map[x][y] = '?';
int size = 1;
size += findLargestIsland(x - 1, y); size += findLargestIsland(x + 1, y); size += findLargestIsland(x, y - 1); size += findLargestIsland(x, y + 1); size += findLargestIsland(x - 1, y - 1); size += findLargestIsland(x - 1, y + 1); size += findLargestIsland(x + 1, y - 1); size += findLargestIsland(x + 1, y + 1);
return size; }
public static int findLargestIslandInMap() { int largestIsland = 0;
for (int i = 0; i < columnsAmount; i++) { for (int j = 0; j < rowsAmount; j++) { if (map[i][j] == 'x') { int currentIslandSize = findLargestIsland(i, j); largestIsland = Math.max(largestIsland, currentIslandSize); } } }
return largestIsland; }
4 Replies
⌛
This post has been reserved for your question.
Hey @Trzech Polaków w teamie?! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Please format your code to make it more readable. For java, it should look like this:
can you send the original problem?
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.