导入外部文件
寻找最佳的球员组合,是总分达到最高
package 基础;
import java.io.FileInputStream;
import java.util.Scanner;
public class 深搜 {
public static int MAX = -1;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new FileInputStream("src//基础//data.txt"));
int[][] nums =new int[20][6];
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 6; j++) {
nums[i][j] = scan.nextInt();
}
}
dfs(nums,1,0,new boolean[20]);
System.out.println(MAX);
}
private static void dfs(int[][] nums, int depth,int sum,boolean[]dic) {
//设置出口
if(depth==6) {
if(sum>MAX) {
MAX = sum;
}
return;
}
//递归处理
for (int i = 0; i < 20; i++) {
if(dic[i])
continue;
int val = nums[i][depth];
dic[i] = true;
dfs(nums,depth+1,sum+val,dic);
dic[i] = false;
}
}
}
运行结果:
data.txt文件
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)