Tenth-Team 01

topic

As a basketball team coach, you need to select one player from position 1 to position 5 from the following list to form the team's starting lineup.
The scores of each player from position 1 to position 5 are shown in the table below. Could you please calculate the maximum possible sum of the scores from the 1st to the 5th of the starting lineup?
Insert picture description here

Idea: Create an array based on the 20 rows and 6 columns above, and input the data through the scanner class. Find the maximum value through dfs backtracking.

See the code for details

public static void main(String[] args) {
    
    
		Scanner input = new Scanner(System.in);
		// 创建一个20行6列的数组
		int[][] score = new int[20][6];
		// 输入
		for (int i = 0; i < score.length; i++) {
    
    
			for (int j = 0; j < score[i].length; j++) {
    
    
				score[i][j] = input.nextInt();
			}
		}
		// 回溯查找最大值
		dfs(score, 1, 0, new HashSet<>());
		System.out.println(max);

	}

	// 找最大值
	static int max = 0;

public static void dfs(int[][] score, int index, int tempSum, HashSet<Integer> set) {
    
    
		// index 代表多少号位,1就是1号位,2就是2号位
		// tempSum是求每种方案的值
		// set用于判重:如果有一位球员在多种号位的值都很大,那么就要进行取舍
		if (index == 6) {
    
    // 若是6,表示人员已满
			max = Math.max(tempSum, max);// 求所有方案中的最大值
			return;
		}
		for (int i = 0; i < score.length; i++) {
    
    
			// 从第1个人开始
			if (!set.contains(i)) {
    
    // 包含该球员就找下一个
				set.add(i);
				dfs(score, index + 1, tempSum + score[i][index], set);
				set.remove(i);
			}
		}
}

Answer: 490

Question summary

This question can be found directly without writing code. It is a problem of seeking the maximum value. It can be done with care.
But if it is compiled into code, you need to think about how to go back and go through all the situations once, and there are no duplicate players.
end.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/109106501
01
01-