题目链接
分析:在DFS暴力破解已经非常熟练的情况下是没有任何难度的.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static int count = 0;
public static void main(String[] args){
Scanner input;
try {
input = new Scanner(new File("D:/123.txt"));
ArrayList<Integer> list = new ArrayList<Integer>();
while(input.hasNext()){
int weidth = input.nextInt();
int height = input.nextInt();
if(height<=20){
list.add(weidth);
}
}
input.close();
System.out.println(dfs(list,0,0,0));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int dfs(ArrayList<Integer> list,int index,int x,int n){
if(x>100){
return Integer.MIN_VALUE;
}
if(index==list.size()){
if(x==100){
return n;
}
return Integer.MIN_VALUE;
}
return Math.max(dfs(list,index+1,x+list.get(index),n+1),dfs(list,index+1,x,n));
}
}
本文介绍了一个基于深度优先搜索(DFS)算法解决礼物盒问题的实现案例。通过对输入文件中宽度小于等于20的盒子进行筛选,并使用DFS算法来寻找能够恰好填满长度为100的盒子组合的最大数量。
809

被折叠的 条评论
为什么被折叠?



