BrainDen.com - Brain Teasers
• 0

# Numbers 25

## Question

Eksempel in the top left corner means Example...

What is the numbers in ABCDEF....

## 1 answer to this question

• 0

Very nice puzzle...

```
public class Main {

public static void main(String[] args) {

int[][] connections = { { 1 }, { 0, 2, 3, 9 }, { 1, 6 }, { 1, 4, 5 },

{ 3, 5, 10 }, { 3, 4, 9, 10 }, { 2, 7, 8, 9 }, { 6, 8 },

{ 6, 7, 9, 11 }, { 1, 5, 6, 8, 10, 11 }, { 4, 5, 9, 11 },

{ 8, 9, 10 } };

int[] values = new int[connections.length];

int[] sums = { 26, 31, 13, 19, 19, 25, 20, 19, 6, 6, 7, 8 };

for (int i = 0; i < values.length; i++) {

values[i] = -1;

}

tryValues(connections, values, sums, 0);

}

private static void tryValues(int[][] connections, int[] values,

int[] sums, int index) {

if (index >= values.length) {

System.out.println(java.util.Arrays.toString(values));

return;

}

for (int i = 0; i < values.length; i++) {

if (!contains(values, i)) {

values[index] = i;

if (isLegal(connections, values, sums)) {

tryValues(connections, values, sums, index + 1);

}

values[index] = -1;

}

}

}

private static boolean isLegal(int[][] connections, int[] values, int[] sums) {

for (int i = 0; i < values.length; i++) {

if (values[i] >= 0) {

int sum = 0;

boolean incomplete = false;

for (int con : connections[i]) {

if (values[con] < 0) {

incomplete = true;

break;

}

sum += values[con];

}

if (!incomplete) {

if (sum != sums[values[i]]) {

return false;

}

}

}

}

return true;

}

private static boolean contains(int[] arr, int num) {

for (int a : arr) {

if (a == num) {

return true;

}

}

return false;

}

}

```

## Create an account

Register a new account