java implements seven-star filling in the 7th Blue Bridge Cup

Keywords: Java

Seven Stars

As shown in Figure 1.png.

Fill in the number of 1-14 on the 14 nodes of Heptagonal Star, without repetition or omission.
It is required that the sum of the four numbers on each line be equal.

Three numbers have been given in the figure.
Please calculate the number to fill in at other locations. The answer is unique.

After filling in, please submit four numbers of green nodes (left to right, separated by spaces)

For example: 12,554,8
Of course, this is not the right answer.

Note: Submit only four numbers separated by spaces, and do not fill in any redundant content.

Answer: 1039 8

import java.util.HashSet;

public class Main {
    public static int sum = 0;
    
    public void swap(int[] A, int i, int j) {
        int temp = A[i];
        A[i] = A[j];
        A[j] = temp;
    }
    
    public void dfs(int[] A, int step) {
        if(step == A.length) {
            int[] count = new int[7];
            count[0] = A[0] + A[1] + A[2] + A[3];
            count[1] = A[0] + A[4] + A[6] + A[9];
            count[2] = A[1] + A[4] + 6 + 14;
            count[3] = A[2] + A[5] + 6 + 11;
            count[4] = A[6] + A[8] + A[10] + 14;
            count[5] = A[7] + A[8] + A[9] + 11;
            count[6] = A[3] + A[5] + A[7] + A[10];
            HashSet<Integer> set = new HashSet<Integer>();
            for(int i = 0;i < 7;i++)
                set.add(count[i]);
            if(set.size() == 1) {
                for(int i = 0;i < A.length;i++)
                    System.out.print(A[i]+" ");
                System.out.println();
            }
            sum++;
            return;
        } else {
            for(int i = step;i < A.length;i++) {
                swap(A, i, step);
                dfs(A, step + 1);
                swap(A, i, step);
            }
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        int[] A = {1,2,3,4,5,7,8,9,10,12,13};
        test.dfs(A, 0);
        System.out.println(sum);
    }
}

Posted by zilem on Tue, 30 Jul 2019 16:41:09 -0700