Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*NQUEEN PROBLEM*/
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- int a[100][100],n,*p[100];
- void prohibit(int,int);
- void notprohibit(int,int);
- void main() {
- int f=0,c,i,j,t=0,b[100];
- printf("\nEnter the number of Queens\n");
- scanf("%d",&n);
- if(n==2 || n==3) {
- printf("\nNull pointer assignment--->No Solutions!!!\n");getch();exit(0);
- }
- for(c=0;c<n;++c) {
- int *r;
- if(!f)
- p[c]=(*(a+c)+0);
- for(r=p[c];r<=&a[c][n-1] && *r==1;++r);
- if(r<=&a[c][n-1]) {
- a[c][r-(*(a+c)+0)]=2;
- prohibit(c,r-(*(a+c)+0));
- p[c]=++r;
- f=0;
- }
- else {
- --c;
- a[c][(p[c]-(*(a+c)+0))-1]=0;
- notprohibit(c,(p[c]-(*(a+c)+0))-1);
- f=1;
- --c;
- }
- }
- for(i=0;i<n;++i) {
- for(j=0;j<n;++j) {
- if(a[i][j]==2) {
- printf(" Q ");
- b[t]=j;
- ++t;
- }
- else
- printf(" * ");
- }
- printf("\n");
- }
- printf("\n\n");
- printf("\nThe Position(s) are=");
- for(t=0;t<n;++t)
- printf(" %d",b[t]);
- getch();
- }
- void prohibit(int i,int j) {
- int k,l,m;
- for(k=i+1,l=j+1,m=j-1;k<n;++k,++l,--m) {
- a[k][j]=1;
- if(l<n)
- a[k][l]=1;
- if(m>=0)
- a[k][m]=1;
- }
- }
- void notprohibit(int i,int j) {
- int k,e,l,m;
- for(k=i+1,l=j+1,m=j-1;k<n;++k,++l,--m) {
- a[k][j]=0;
- if(l<n)
- a[k][l]=0;
- if(m>=0)
- a[k][m]=0;
- }
- for(e=0;e<i;++e)
- prohibit(e,(p[e]-(*(a+e)+0))-1);
- }
Add Comment
Please, Sign In to add comment