import java.util.*;
public class Dyn {
public static void main(String[] args) {
customBreadthFirstSearch();
}
private static int numberOfCheckCalls = 0;
public static void customBreadthFirstSearch() {
int n = 5;
Deque<int[]> solutions = new ArrayDeque<>();
solutions.addLast(new int[]{11, 11, 11, 11, 11});
int best = 1000;
while (!solutions.isEmpty()) {
int[] solution1 = solutions.removeFirst();
int check = checkSolution(solution1);
numberOfCheckCalls++;
System.out.println(Arrays.toString(solution1) + " " + check + " (was " + best + ")");
if (check == 0) {
System.out.printf(" %d %n", 86);
System.out.printf(" %d %d %n", 47, 39);
System.out.printf(" %d %d %d %n", solution1[0], solution1[1], solution1[2]);
System.out.printf(" %d %d %d %d %n", 12, solution1[3], solution1[4], 5);
System.out.println("numberOfCheckCalls = " + numberOfCheckCalls);
return;
}
if (check > best) {
continue;
}
if (check < best) {
best = check;
}
int[] solution2 = new int[n];
System.arraycopy(solution1, 0, solution2, 0, n);
for (int i = 0; i < n; i++) {
solution2[i]++;
int[] solution3 = new int[n];
System.arraycopy(solution2, 0, solution3, 0, n);
solutions.addLast(solution3);
}
}
}
private static int checkSolution(int[] a) {
int[] b = {
86,
47, 39,
a[0], a[1], a[2],
12, a[3], a[4], 5
};
int[] result = {
b[3] + b[4] - b[1],
b[4] + b[5] - b[2],
b[6] + b[7] - b[3],
b[7] + b[8] - b[4],
b[8] + b[9] - b[5],
};
int sum = 0;
for (int r : result) {
sum += Math.abs(r);
}
return sum;
}
}