题目

【问题描述】 
作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员, 组成球队的首发阵容。
每位球员担任 1 号位至 5 号位时的评分如下表所示。
请你计算首发阵容 1 号位至 5

蓝桥杯2019年真题:组队_文本文件

(如果你把以上文字复制到文本文件中,请务必检查复制的内容是否与文档中的一致。
在试题目录下有一个文件 team.txt,内容与上面表格中的相同, 请注意第一列是编号)
【答案提交】
这是一道结果填空的题,你只需要算出结果后提交即可。
本题的结果为一 个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

team.txt文件内容

1 97 90 0 0 0
2 92 85 96 0 0
3 0 0 0 0 93
4 0 0 0 80 86
5 89 83 97 0 0
6 82 86 0 0 0
7 0 0 0 87 90
8 0 97 96 0 0
9 0 0 89 0 0
10 95 99 0 0 0
11 0 0 96 97 0
12 0 0 0 93 98
13 94 91 0 0 0
14 0 83 87 0 0
15 0 0 98 97 98
16 0 0 0 93 86
17 98 83 99 98 81
18 93 87 92 96 98
19 0 0 0 89 92
20 0 99 96 95 81

答案

490
其实人工肉眼也能看出来,但是毕竟是计算机的比赛,先看下如果是暴力破解的话
时间复杂度能不能成功,20^5=3200000,还好,不是很大,直接暴力了

package competition3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class Team
{
public static int count=0;
public static int visited[]=new int[20];
public static int[][] arr=new int[20][6];

public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader(new FileReader(new File("src/competition3/team.txt")));

String temp;
int index=0;
while((temp=in.readLine())!=null)
{
String[] split = temp.split(" ");
for (int x=0;x<split.length;x++)
{
arr[index][x]=Integer.parseInt(split[x]);
}
index++;
}
//上面将所有的数据都读取完毕
dfs(1, 0);
System.out.println(count);
in.close();
}
public static void dfs(int index,int sum)
{
if(index==5+1)
{
count=(count>sum)?count:sum;
return;
}
for(int x=0;x<visited.length;x++)
{
if(visited[x]==0)
{
visited[x]=1;
dfs(index+1, sum+arr[x][index]);
visited[x]=0;
}
}
}
}