#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void queens(int i);
bool promising(int i);

int n;
int *col;

int main()
{

 printf(“Insert Number…n”);
 printf(“Insert : “);
 scanf(“%d”, &n);

 col = (int *)malloc(n * sizeof(int));
 queens(0);

 return 0;
}

void queens(int i)
{
 int j, tmp;

 if(promising(i))
  if(i == N) {
   for(tmp = 1; tmp <= N; tmp++)
    printf(“%d “, col[tmp]);
   printf(“n”);
  }
  else
   for(j = 1; j <= N; j++) {
    col[i + 1] = j;
    queens(i + 1);
   }
}

bool promising(int i)
{
 int k;
 bool result;

 k = 1;
 result = true;

 while(k < i && result) {
  if(col[i] == col[k] || abs(col[i] – col[k]) == i – k)
   result = false;
  k++;
 }
 return result;
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.