here u go:
int no_of_solutions = 0;
int n = 4;
int x[20];
typedef struct
{
int value[20];
}solutions;
solutions mySolutions[20];
void print(int k)
{
var i;
for (i=1;i<k+1;i++)
mySolutions[no_of_solutions].value[i] = x[i];
}
int solution(int k)
{
return k==n;
}
int possible(int k)
{
var i;
for (i=1;i<k;i++)
if (x[i]==x[k] || abs(x[i]-x[k])==k-i)
return 0;
return 1;
}
void back(int k)
{
if (solution(k))
{
print(k);
no_of_solutions++;
}
else
for (x[k+1]=1; x[k+1]<=n; x[k+1]++)
if (possible(k+1))
back(k+1);
}
void main()
{
level_load(NULL);
back(0);
wait(-1);
}
Here, this follows the chessboard example. It computes the possible arrangement of queens on a chessboard 4 x 4. (u can change the size by changing the value of n). In such a way that no two queens are next to each other.
Here, I have saved the results in a struct array. The results is as follows:
mySolutions[1] = {2 4 1 3}
mySolutions[2] = {3 1 4 2}
with n=4, there are only 2 possible solutions.
for {2 4 1 3} this means the queen should be positioned at (1,2), (2,4), (3,1) and (4,3). We dont keep position of the row. the first element corresponds to the first row, second to the second row, etc..
now u can use this function to create a visual. Make a chessboard in 3D. And using the positions here u place them on the chessboard to show the combinations.