Hi I want to make a 3 x 3 magic square in C
using backtracking
(as in the 4 queens exercise) with recursivity
.
In addition, I must enter the maximum value that this magic square will have inside, for example if I enter m = 26, my table should look something like this:
[22,8,21]
[16,17,18]
[13,26,12]
as it should be done by backtracking
, that is one possible solution of many, currently I have a simple code of 3 loops to perform all the possible combinations by entering the value of M.
attached code:
#include <stdio.h>
#include <string.h>
#define N 10
void print (int * num, int n)
{
int i;
for (i = 0; i <n; i ++)
printf ("% d", num [i]);
printf ("\ n");
}
int main ()
{
int num [N];
int * ptr;
int temp;
int i, m, j;
int n = 3;
printf ("\ nlimite:");
scanf ("% d", & m);
for (int i = 1; i <= m; ++ i)
{
for (int j = 1; j <= m; ++ j)
{
for (int k = 1; k <= m; ++ k)
{
permutations ++;
printf ("%i,%i,%i\n", i, j, k);
}
}
}
}
How can I transform this code to be recursive? and without repeating the first values, for example [1,1,1] [16,16,16] since this will allow me to create the possible rows and columns to elaborate the magic square.
and finally to be able to print all the possible solutions that are correct.
solution 1 solution N
[4,9,2] [22,8,21]
[3,5,7] [16,17,18]
[8,1,6] ... [13,26,12]
for compilation I use MingGW - gcc
on windows
, in advance thanks a lot for the help