N Queen problem is a classic constraint satisfaction problem based on chess game.
The idea is how to put N numbers of Queen in N x N chessboard without giving them any chance to kill each other in one turn.

This article is specially dedicated for those with interest in artificial intelligence subject. The main topic is N Queen Problem, a classical problem of constraint satisfaction. However, this problem will always great to be discussed all the time. If you are a beginner in this section, please read the preface on the left side of this page.

I have made many versions of code in some different programming languages. Here is one of them, written in C++, compiled using Turbo C++ 1.0 from Borland. The running time is not really short, but this code has a very good N-Time curve where it is smooth and clean from exceptions and errors. Soon, I will publish my newer and better codes here, too. Please be patient.
You can use, compile, analyze and change any part of the source code below. If you find out how to make this algorithm works better, please send me an e-mail containing the change you have made and some explanations about your new improvements.

```// N Queen Problem : versi 8  by Robert Setiadi
// This code is able to generate all pattern combinations
// and display the backtracking counter.

#include&lt;stdio.h&gt;
#include&lt;iostream.h&gt;
#include&lt;conio.h&gt;
#include&lt;dos.h&gt;
#include&lt;graphics.h&gt;
#include&lt;stdlib.h&gt;

int jawab[1001];
char boleh[1001];
int posisi,i,j,n,counter,tampil;
unsigned long int jback;
struct time t1,t2;
unsigned long kemungkinan;
char kmb,pertama,terus;

void generate_sah() {
int i,dx;
for(i=1;i&lt;=n;i++) boleh[i]=1;
for(i=1;i&lt;posisi;i++) {
boleh[jawab[i]]=0;dx=posisi-i;
if(jawab[i]-dx&gt;=1) boleh[jawab[i]-dx]=0;
if(jawab[i]+dx&lt;=n) boleh[jawab[i]+dx]=0;
}
}

void waktu(char tanda) {
struct time t;
if(tanda==3) {
printf("Starting time : %2d:%02d:%02d.%02d\n",
t1.ti_hour, t1.ti_min, t1.ti_sec, t1.ti_hund);
printf("Current time  : %2d:%02d:%02d.%02d\n",
t2.ti_hour, t2.ti_min, t2.ti_sec, t2.ti_hund);
}
else gettime(&amp;t);
if(tanda==1) t1=t; if(tanda==2) t2=t;
}

void main() {
clrscr();
cout&lt;&lt;"Artificial Intelligence : Queen Problem - version 8.0"&lt;&gt;n;
} while (n1000);
do {
cout&lt;&lt;"Display all possible combinations (Y/N) ? ";
kmb=getche();
} while (!(kmb=='y' || kmb=='Y' || kmb=='n' || kmb=='N'));
if(kmb=='n' || kmb=='N') pertama=1; else pertama=0;
waktu(1);cout&lt;&lt;endl;
for(i=1;i&lt;=n;i++) jawab[i]=0;                       // initialization
counter=0;posisi=1;kemungkinan=1;terus=1;jback=0;
do {
cout&lt;&lt;endl;
do {
if(jawab[posisi]&gt;=n) {
if(posisi!=1) jawab[posisi]=0;
posisi--;
counter--;
}
else {
j=jawab[posisi]+1;
generate_sah();
while(!boleh[j] &amp;&amp; j&lt;=n) {
j++;
}
if(j&lt;=n) {
jawab[posisi]=j;
counter++;
posisi++;
}
else {
jawab[posisi]=0;
posisi--;
counter--;
jback++;
}
}
if(counter=0) terus=1; else terus=0;
if(counter==-1 &amp;&amp; !pertama) terus=0;
} while(terus);
if(counter==n) {
waktu(2);
tampil=0;
if(!pertama) cout&lt;&lt;"Kemungkinan ke "&lt;&lt;kemungkinan&lt;&lt;" :\n";
cout&lt;&lt;setiosflags(ios::left);
for(i=1;i&lt;=n;i++) {
cout&lt;&lt;'Q'&lt;&lt;setw(4)&lt;&lt;i&lt;&lt;"=("&lt;&lt;setw(4)&lt;&lt;i&lt;&lt;','&lt;&lt;setw(4)&lt;&lt;jawab[i]&lt;&lt;")  ";
tampil++;if(tampil&gt;=4) { cout&lt;&lt;endl; tampil=0; }
}
if(!pertama) {
cout&lt;&lt;"\nPress any key ... ";
kmb=getch();
cout&lt;&lt;endl;
kemungkinan++;
posisi=1;
counter=0;
for(i=1;i&lt;n;i++) jawab[i]--;
}
}
} while(counter!=-1 &amp;&amp; !pertama);
cout&lt;&lt;endl;