N queen problem code in C Language
Write a program in C Language to Implement N queen problem using C Language and print the number of Solution to the problem as output.
Recursive backtracking algorithm, iterative backtracking method queens problem. Sum of subsets and Graph coloring, Hamiltonian cycle and Knapsack Problem etc.
N queen problem code
#include #include #include #include int check(int *s,int k) { for(int i=0;i<k;++i) <br=""> { if( (*(s+i)==*(s+k)) || ( abs(i-k)==abs(*(s+i)-*(s+k)) ) ) return 1; } return 0; } void display(int * s,int n) { printf("Solution\n"); for(int i=0;i<n;++i) <br=""> { printf("\t%d",i); } printf("\n"); for(i=0;i<n;++i) <br=""> { printf("%d\t",i); for(int j=0;j<n;++j) <br=""> { if(*(s+i)==j) printf("x\t"); else printf("\t"); } printf("\n"); } } void queen(int *s,int n) { int k; int count=0; while(*(s+0) <= n-1) { k=1; while(k>0) { while(*(s+k) ++(*(s+k)); if(*(s+k)<n) <br=""> { if(k==n-1) { count++; display(s,n); //if(count%3==0) getch(); ++(*(s+k)); } else k++; } else { *(s+k)=0; k--; ++*(s+k); } } } printf("\n Total no of solutions are:%d",count); } void main() { int n; clrscr(); printf("Enter the no of queens\n"); scanf("%d",&n); int *s; s=(int *)calloc(1,2*n); queen(s,n); getch(); }