Carter
Carter
Explore posts from servers
JCHJava Community | Help. Code. Learn.
Created by Trzech Polaków w teamie? on 10/30/2024 in #java-help
How to find the biggest island in the file
can you send the original problem?
7 replies
JCHJava Community | Help. Code. Learn.
Created by Trzech Polaków w teamie? on 10/30/2024 in #java-help
How to find the biggest island in the file
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;
}
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;
}
7 replies
JCHJava Community | Help. Code. Learn.
Created by OMIDD on 10/11/2024 in #java-help
recursion
if you don't understand recursion, I would try to visualize the call stack of a java program. When you call methods inside methods, the context of the original method is saved in a stack data structure. When a recently called method returns, it's popped off the stack. There is nothing special about recursion, it's just calling a method and that method call happens to be in the same method called.
13 replies
JCHJava Community | Help. Code. Learn.
Created by rainymc_ on 9/15/2024 in #java-help
No response.
it should be scanner.nextDouble() you're calling scanner.next()
21 replies
JCHJava Community | Help. Code. Learn.
Created by rainymc_ on 9/15/2024 in #java-help
No response.
look up how to take in a Double
21 replies