My professor gave us an assignment where I need to search around a given point in a grid for all other spots that makes up a group (in this example I need to find the number of spots that in a "L" shape within the problem).
So the grid is 10x10, and my professor has given us a spot to start with. My professor gave us an idea which is to check the neighboring spots, and add it to a set, if the spot is newly found (which will be in the set) then recursively call that method.
private Spot findSpots(Set<Spot> spots, Set<Spot> activeSpots, Spot initial) {
Spot newSpot = null;
Set<Spot> activeSet = new HashSet<Spot>();
checkAround(activeSet, new Spot(initial.i - 1, initial.j));
checkAround(activeSet, new Spot(initial.i + 1, initial.j));
checkAround(activeSet, new Spot(initial.i, initial.j - 1));
checkAround(activeSet, new Spot(initial.i, initial.j + 1));
for (Spot spot : activeSet) {
newSpot = findSpots(spots, activeSpots, spot);
if (grid[newSpot.i][newSpot.j] == true)
spots.add(newSpot);
}
return newSpot;
}
private boolean checkAround(Set<Spot> spots, Spot spot) {
if (!spots.contains(spot)) {
spots.add(spot);
return true;
}
return false;
}
I know I need a boundary condition (or else I will get stackoverflow exceptions), but I need help in the logic.