교수님은 그룹을 구성하는 다른 모든 지점에 대해 그리드의 특정 지점을 검색해야하는 과제를 주셨습니다 (이 예에서는 "L "문제의 모양).탐욕스러운 재귀 검색
그리드는 10x10이고 내 교수는 우리에게 시작점을주었습니다. 교수님은 우리에게 이웃 한 장소를 확인하고, 그 지점이 새로 발견되면 (세트에 포함될 것입니다) 그 세트에 그것을 추가하는 아이디어를주었습니다. 그런 다음 재귀 적으로 그 방법을 호출합니다.
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;
}
경계 조건이 필요합니다. 그렇지 않으면 stackoverflow 예외가 발생하지만 논리에 도움이 필요합니다. 내가 경계 조건을 필요로 알고
변수 이름에 더 많은 생각을하면 코드를 읽기 쉽고 수정하기가 쉽습니다. "initialSpots"와 같은 것조차도이 모든 다양한 스폿 구조를 추론하려고 할 때 훨씬 덜 혼란 스럽습니다. – dahui
I 다른 사람들이 쉽게 읽을 수 있도록 일부 변수의 이름을 변경했습니다. –
Spot 클래스에 부울 멤버 변수 또는 함수를 추가하거나 "hasBeenSearched"속성을 추가하고이 지점에서 이미 함수를 호출 한 경우 true로 설정하면 함수가 호출되지 않도록 할 수 있습니다. Spot.hasBeenSearched 만약 spot이 있다면 – dahui