What is CrazyEngico.com (CE)?

CrazyEngico (CE) is a one-stop solution for interview questions and answers, domain knowledge, e-learning, tutorials, articles and many more learning material.
e-learning - Intelligent e-learning tutorials and articles
Domain Knowledge Learn about various business domains/verticals such as Banking, Financial, Insurance, Healthcare and many more...
Interview Questions and Answers - Post your questions and answers. 1000+ database of ready questions and answers available to prepare for an interview.
Tutorials - 300+ video tutorials from beginner to expert level.
Articles - 300+ technical articles to understand concepts.
Forum - A technical forum to post your queries/responses.

Ads
New Solution For N-Queens Problem
Views: 1957 | Votes: 12        1

New Solution For N-Queens Problem

Author: siamak solat

source code in c++ :

#include
#include
#include

char i[100],j[100];
int n,k,p,q,z,l,m,cs,cc,Tc;
char ch;
main()
{
textmode(64);
Tc=10;
textbackground(0);clrscr();textcolor(Tc);
A:cprintf("Enter an integer(n<=49): ");
cin>>n;
if (n>49)
{ clrscr();gotoxy(1,1);goto A; }
cs=1;
if (n % 2!=0 && n % 3==0)
cc=1;
else
cc=2;
B: clrscr();
p=1;
q=1;
if(n % 2==0){
for( z=1;z<=floor(n/2);z++)
{//for
gotoxy(p,q);
for( l=1;l<=floor(n/2);l++)
{textcolor(15);cprintf("Û");
textcolor(0);cprintf("Û");}
q=q+1;
gotoxy(p,q);
for( l=1;l<=floor(n/2);l++)
{textcolor(0);cprintf("Û");
textcolor(15);cprintf("Û");}
q=q+1;
}//for
}//if
else if(n % 2!=0){
for( z=1;z<=floor(n/2);z++)
{//for
gotoxy(p,q);
for( l=1;l<=floor(n/2);l++)
{textcolor(15);cprintf("Û");
textcolor(0);cprintf("Û");}

textcolor(15);cprintf("Û");q=q+1;
gotoxy(p,q);
for( l=1;l<=floor(n/2);l++)
{textcolor(0);cprintf("Û");
textcolor(15);cprintf("Û");}

textcolor(0);cprintf("Û");q=q+1;
}//for
p=1;
gotoxy(p,q);
for( l=1;l<=floor(n/2);l++)
{textcolor(15);cprintf("Û");
textcolor(0);cprintf("Û");}

textcolor(15);cprintf("Û");
}//else if
/**************************************************************************/
if (n % 2!=0)
{
m=n % 3;
if (m==1 || m==2)
{//if
i[1]=1;
j[1]=cc;
gotoxy(i[1],j[1]);
textcolor(Tc);
cprintf("*");
k=2;

while(i[k-1]!=n)
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
j[k]=j[k] % n;
gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while

gotoxy(53,1);
cprintf("N=%i",n);
gotoxy(53,3);
cprintf("Case:%i",cs);
gotoxy(53,5);
cprintf("Continue: Enter");
gotoxy(53,7);
cprintf("Next Integer: a");
gotoxy(53,9);
cprintf("Exit: Esc");
cs=cs+1;
cc=cc+1;

if (cc<=(n-2))
{
gotoxy(1,(n+1));
ch=getch();

if(ch==char(13))
goto B;
else if(ch==char(27))
goto D;
else if(ch==char(97)){
gotoxy(53,11);goto A;}

}

if (cc>(n-2))
{
gotoxy(53,11);
goto A;
}
}
}
/**************************************************************************/
if(n % 2!=0)
{
if (m==0)
{//if
i[1]=1;
j[1]=cc;
gotoxy(i[1],j[1]);
textcolor(Tc);
cprintf("*");
k=2;

if (cc % 2==0)
while(i[k-1]!=(n))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
{//if
i[k]=i[k]+1;
j[k]=(j[k]-n);
}//if

gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
int ccc1=cc/3;
//int cccc1=floor(ccc1);
if (cc % 2!=0 && ccc1 % 2==0)
while(i[k-1]!=(n))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
{//if
i[k]=i[k]+1;
j[k]=(j[k]-n);
}//if

gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
int ccc2=cc/3;
//int cccc2=floor(ccc2);
if (cc % 2!=0 && ccc2 % 2!=0)
while(i[k-1]!=(n-1))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
{//if
i[k]=i[k]+1;
j[k]=(j[k]-n);
}//if

gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while

gotoxy(53,1);
cprintf("N=%i",n);
gotoxy(53,3);
cprintf("Case:%i",cs);
gotoxy(53,5);
cprintf("Continue: Enter");
gotoxy(53,7);
cprintf("Next Integer: a");
gotoxy(53,9);
cprintf("Exit: Esc");
cs=cs+1;
cc=cc+1;

if(cc==6)
cc=cc+1;

if (cc<=(n-2))
{
gotoxy(1,(n+1));
ch=getch();
if(ch==char(13))
goto B;
else if(ch==char(27))
goto D;
else if(ch==char(97)){
gotoxy(53,11);goto A;}

}

if (cc>(n-2))
{
gotoxy(53,11);
goto A;
}
}//if
}//if
/**************************************************************************/
if (n % 2==0)
{//if
m=n % 3;
if (m==0 || m==1)
{//if
i[1]=1;
j[1]=cc;
gotoxy(i[1],j[1]);
textcolor(Tc);
cprintf("*");
k=2;if(cc % 2==0) {
int ccc3=cc/2;
//int cccc3=floor(ccc3);
if(ccc3 % 2 !=0)   {
while(i[k-1]!=n)
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
if(j[k]-2==n)
{
j[k]=j[k] % n -1;
}
else
{
i[k]=i[k]+1;j[k]=j[k]-n+1;
}
gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
}
int ccc4=cc/2;
//int cccc4=floor(ccc4);
if(ccc4 % 2==0)
{
while(i[k-1]!=(n-1))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
if(j[k]-2==n)
{
j[k]=j[k] % n -1;}

else
{
i[k]=i[k]+1;j[k]=j[k]-n+1;
}
gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
}
}

if(cc % 2!=0)
{  int ccc5=cc/2;
//int cccc5=floor(ccc5);
if(ccc5 % 2 !=0)   {
while(i[k-1]!=n)
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
if(j[k]-2==n)
{
j[k]=j[k] % n -1;}
else
{
i[k]=i[k]+1;j[k]=j[k]-n+1;
}
gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
}
int ccc6=cc/2;
//int cccc6=floor(ccc6);
if (ccc6 !=0) {
if(ccc6 % 2==0)
{
while(i[k-1]!=(n-1))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
if(j[k]-2==n)
{
j[k]=j[k] % n -1;}
else
{
i[k]=i[k]+1;j[k]=j[k]-n+1;
}
gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
}
}
}

gotoxy(53,1);
cprintf("N=%i",n);
gotoxy(53,3);
cprintf("Case:%i",cs);
gotoxy(53,5);
cprintf("Continue: Enter");
gotoxy(53,7);
cprintf("Next Integer: a");
gotoxy(53,9);
cprintf("Exit: Esc");
cs=cs+1;
cc=cc+1;

if (cc<=(n-2))
{
gotoxy(1,(n+1));
ch=getch();

if(ch==char(13))
goto B;
else if(ch==char(27))
goto D;
else if(ch==char(97)){
gotoxy(53,11);goto A;}
}

if (cc>(n-2))
{
gotoxy(53,11);
goto A;
}
}//if

if (m==2)
{//if
i[1]=1;
j[1]=cc;
gotoxy(i[1],j[1]);
textcolor(Tc);
cprintf("*");
k=2;
int ccc7=cc/3;
//int cccc7=floor(ccc7);
if (cc % 2==0)
if ((cc==2 || ccc7 % 2!=0))
{
while(i[k-1]!=(n))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;

if (j[k]>n)
if(j[k]-2==n)
{//if
i[k]=i[k]+1;
j[k]=j[k] -(n+1);
}
else
j[k]=(j[k]-(n+1))+4;

gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while
}
int ccc8=cc/3;
//int cccc8=floor(ccc8);
if (cc % 2==0 && ccc8 % 2==0)
{
cc=cc+1;
goto C;
}

if (cc % 2!=0)
C:while(i[k-1]!=(n))
{//while
i[k]=i[k-1]+1;
j[k]=j[k-1]+2;
if (j[k]>n)
if(j[k]-2==n)
{//if
i[k]=i[k]+1;
j[k]=j[k] -(n+1);
}
else
j[k]=(j[k]-(n+1))+4;

gotoxy(i[k],j[k]);
textcolor(Tc);
cprintf("*");
k=k+1;
}//while

gotoxy(53,1);
cprintf("N=%i",n);
gotoxy(53,3);
cprintf("Case:%i",cs);
gotoxy(53,5);
cprintf("Continue: Enter");
gotoxy(53,7);
cprintf("Next Integer: a");
gotoxy(53,9);
cprintf("Exit: Esc");
cs=cs+1;
cc=cc+1;
if (cc<=(n-2))
{
gotoxy(1,(n+1));
ch=getch();

if(ch==char(13))
goto B;
else if(ch==char(27))
goto D;
else if(ch==char(97)){
gotoxy(53,11);goto A;}
}

if (cc>(n-2))
{
gotoxy(53,11);
goto A;
}

}//if

}//if
return 0;
D:}

Article Source: http://www.articlesbase.com/programming-articles/new-solution-for-n-queens-problem-770215.html

About the Author

Software Engineer


Copyright © 2017 CrazyEngico.com. All rights reserved.
CrazyEngico.com is not responsible for the content and CrazyEngico.com does not evaluate or guarantee the accuracy of any CrazyEngico.com content.
Please read our terms of service agreement before using this site.

feedback