1、运用场景
- 回溯算法:通过穷举所有可能的解来找出所有满足条件的解的算法
- 复杂度:通常为指数级别
- 优缺点:
-
-
- 缺点:时间复杂度高,递归深度过大,可能会导致栈溢出等问题
-
2、流程图

3、代码示例
import java.util.ArrayList;
import java.util.List;
public class ResourceCombinationGenerator {
public class Resource {
String name;
int amount;
public Resource(String name, int amount) {
this.name = name;
this.amount = amount;
}
@Override
public String toString() {
return name + "(" + amount + ")";
}
}
private static final String RESOURCE_A = "A";
private static final String RESOURCE_B = "B";
public void backtrack(int start,List<Resource> resources,List<Resource> currentCombination,List<List<Resource>> result) {
if (currentCombination.size() > 3) {
return ;
}
if (!currentCombination.isEmpty() &&
currentCombination.size() <= 3 &&
currentCombination.stream().mapToInt(resource -> resource.amount).sum() >= 100 &&
currentCombination.stream().mapToInt(resource -> resource.amount).sum() <= 200 &&
currentCombination.stream().anyMatch(resource -> RESOURCE_A.equals(resource.name)) &&
!currentCombination.stream().anyMatch(resource -> RESOURCE_B.equals(resource.name))) {
result.add(new ArrayList<>(currentCombination));
}
for (int i = start; i < resources.size(); i++) {
Resource resource = resources.get(i);
if (RESOURCE_B.equals(resource.name)) {
continue;
}
currentCombination.add(resource);
backtrack(i + 1,resources,currentCombination,result);
currentCombination.remove(currentCombination.size() - 1);
}
}
@Test
public void main() {
List<Resource> resources = new ArrayList<>();
resources.add(new Resource("A", 50));
resources.add(new Resource("B", 30));
resources.add(new Resource("C", 60));
resources.add(new Resource("D", 90));
resources.add(new Resource("E", 120));
List<List<Resource>> combinations=new ArrayList<>();
backtrack(0,resources,new ArrayList<>(),combinations);
System.out.println(combinations);
}
}