I have this pseudo-code that I am supposed to use to come up with the coded algorithm
for filling a 3x3 Magic Square. I can’t seem to grasp my head around this pseudo code
for some reason. Could somebody help me with this?
pseudo-code
recursive_funtion(position) {
for number from 1 to 9, not used elsewhere already {
put this number on this position, make it unavailable
if solution valid {
if solution complete {
done, show solution
}else{
recursive_function(next_position)
}
}
(make this space blank again, and the number available)
}
}
My code so far (Completely wrong im sure)
int[][] square = new int[3][3];
ArrayList<Integer> used_nums = new ArrayList<Integer>();
public MagicSquare()
{
rec_backtrack(0);
print_square();
}
public void rec_backtrack(int level)
{
int step = 0;
for(int i = 1; i < 10; i++)
{
if(!used_nums.contains(i))
{
square[step][level] = i;
used_nums.add(i);
step++;
}
if(step == 3) //we have inserted to all 3 spots now, check for solution
{
if((square[0][level] + square[1][level] + square[2][level]) == 15) //acceptable solution
{
rec_backtrack(level++);
}
else
{
step--;
}
}
}
}