https://www.topcoder.com/community/competitive-programming/tutorials/how-to-find-a-solution/
9 numbers need to be arranged in a magic number square. A magic number square is a square of numbers that is arranged such that every row and column has the same sum. You are given 9 numbers that range from 0 to 9 inclusive. Return the number of distinct ways that they can be arranged in a magic number square. Two magic number squares are distinct if they differ in value at one or more positions.
Problem hints: Only 9 numbers are given at most; and every distinct way (configuration) to arrange the numbers so that they form a magic number square should be found.
These are the main properties of a Backtracking problem. If you have observed them – think about the code. You can generate all permutations of numbers and for each of them check if it forms a magic square. If so – add it to the answer. Note that it must be unique. A possible way to do that – is to have a list of earlier found configurations, thus for each new magic square check if it exists in that list and if it doesn’t – add it to the answer. There will not be many distinct magic squares, thus no additional problems will appear when applying this method.
private HashSet<Integer> counted;
public int combos(int[] numbers) {
counted = new HashSet<Integer>();
uniqueMagicSquarePermutations(numbers, 0);
return counted.size();
}
private boolean isMagicSquare(int[] permutation) {
int size = (int) Math.sqrt(permutation.length);
int sum = rowSum(permutation, 0, size);
for (int i = 1; i < size; i++) {
if (sum != rowSum(permutation, i, size)) return false;
if (sum != columnSum(permutation, i, size)) return false;
}
return true;
}
private int columnSum(int[] permutation, int start, int length) {
int sum = 0;
for (int i = start; i < permutation.length; i += length) {
sum += permutation[i];
}
return sum;
}
private int rowSum(int[] permutation, int start, int length) {
int sum = 0;
for (int i = start * length; i < start * length + length; i++) {
sum += permutation[i];
}
return sum;
}
void uniqueMagicSquarePermutations(int[] seq, int index) {
if (index == seq.length) {
if (isMagicSquare(seq)) {
Integer value = 0;
for (int n : seq) value = value * 10 + n;
counted.add(value);
}
} else {
for (int i = index; i < seq.length; i++) {
swap(index, i, seq);
uniqueMagicSquarePermutations(seq, index + 1);
swap(index, i, seq);
}
}
}
private void swap(int i, int j, int[] a) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
A magic square is a two dimensional grid of numbers where every row and column add up to the same amount. In this problem, the grid is always 3x3. If we number the positions as such:
Then, by knowing the values of squares 1, 2, 3, 4, and 5 - we can determine what all the remaining squares must be. Square 1+ Square 2+ Square 3 = the value of the magic square - call this msNum. Therefore, Square 6 =msNum- Square 4 - Square 5. Square 7 =msNum- Square 1 - Square 2, etc.
This solutions uses a brute force approach. Lines 139-164 loop through all possible values for squares 1,2,3,4, and 5. For each combination, it calls generateMagicSquare() which creates as many magic squares as possible using the remaining numbers. The if-statements immediately following each for (i.e. line 43) ensure that the same index do not get used multiple times.
generateMagicSquare() creates as many magic squares as possible, given the fixed squares provided and the set of numbers from which to choose. It creates a new list called availableMaster that holds all the number that were not assigned to those fixed squares, and then uses a copy of that named availableCurrent for each iteration through the loop at line 57. The loop attempts to fill in legal values for squares 6, 7, 8, and 9. If that's possible, then we have a good magic square.
The method msValue() on line 104 creates a unique identifier for each possible magic square. It does this by simply assigning each number in the square to a tens place and returns a long. So if the square was the example given at the very top of this notes sections, the result would be: 123456789. Since this identifier is added to a Set (line 89-91) it ensures that we don't count duplicates