ALGORITHM
N Queens problem solving using BackTracking in C Programming
N Queens problem solving using BackTracking in C Programming
#include
#include
#include
#define MAX=10
int fnPlace(int iK,int iI,int iX[10])
{
int iJ;
for(iJ=1;iJ<=iK-1;iJ++)
{
if(iX[iJ]==iI||abs(iJ-iK)==abs(iX[iJ]-iI))
return 0;
}
return 1;
}
void fnQueens(int iK,int iN)
{
int iI,iJ,iL;
static int iCount,iX[10];
for(iI=1;iI<=iN;iI++)
{
if(fnPlace(iK,iI,iX))
{
iX[iK]=iI;
if(iK==iN)
{
printf("\nFeassible solution : %d",++iCount);
printf("\nSolutions are : ");
for(iJ=1;iJ<=iN;iJ++)
printf(" %d ",iX[iJ]);
for(iJ=1;iJ<=iN;iJ++)
{
printf("\n");
for(iL=1;iL<=iN;iL++)
{
if(iL==iX[iJ])
printf(" X ");
else
printf(" 0 ");
}
}
}
else
fnQueens(iK+1,iN);
}
}
}
void main()
{
int iNo;
clrscr();
printf("Enter the no of Queenes : ");
scanf("%d",&iNo);
fnQueens(1,iNo);
getch();
}
/*
Enter the no of Queenes : 4
Feassible solution : 1
Solutions are : 2 4 1 3
0 X 0 0
0 0 0 X
X 0 0 0
0 0 X 0
Feassible solution : 2
Solutions are : 3 1 4 2
0 0 X 0
X 0 0 0
0 0 0 X
0 X 0 0
*/
#include
#include
#define MAX=10
int fnPlace(int iK,int iI,int iX[10])
{
int iJ;
for(iJ=1;iJ<=iK-1;iJ++)
{
if(iX[iJ]==iI||abs(iJ-iK)==abs(iX[iJ]-iI))
return 0;
}
return 1;
}
void fnQueens(int iK,int iN)
{
int iI,iJ,iL;
static int iCount,iX[10];
for(iI=1;iI<=iN;iI++)
{
if(fnPlace(iK,iI,iX))
{
iX[iK]=iI;
if(iK==iN)
{
printf("\nFeassible solution : %d",++iCount);
printf("\nSolutions are : ");
for(iJ=1;iJ<=iN;iJ++)
printf(" %d ",iX[iJ]);
for(iJ=1;iJ<=iN;iJ++)
{
printf("\n");
for(iL=1;iL<=iN;iL++)
{
if(iL==iX[iJ])
printf(" X ");
else
printf(" 0 ");
}
}
}
else
fnQueens(iK+1,iN);
}
}
}
void main()
{
int iNo;
clrscr();
printf("Enter the no of Queenes : ");
scanf("%d",&iNo);
fnQueens(1,iNo);
getch();
}
/*
Enter the no of Queenes : 4
Feassible solution : 1
Solutions are : 2 4 1 3
0 X 0 0
0 0 0 X
X 0 0 0
0 0 X 0
Feassible solution : 2
Solutions are : 3 1 4 2
0 0 X 0
X 0 0 0
0 0 0 X
0 X 0 0
*/
PLZ INSERT HEADER..
ReplyDeleteto tu kar de!!!!!!!!
Deleteit is a waste of time ....no expln is given...........
ReplyDeletebheja fry
Deletefine...yar
ReplyDeleteDude this is really awesome......i really cant thank u enough ..u saved me..thumbs up..!!!!
ReplyDeletetereko to bacha liya lekin hame to duba diya........
Deleteloduchand tu bhi hoshari jhaad ra hai samaj to tuje bhi nahi aaya!!!!!!!!!
gandue chutiye...
Deleteyaar iski explanation toh dedo
ReplyDeletemadarchod
ReplyDeletemaa ka bhosuda tera, bhadwa chodd, lowde ke baal. jaa apni maa chuda aur apni beti,behan ko mere paas bhej, unko himalaya le jaake itna chodunga itna chodunga, jab tak waha ka burf pigal naa jae, tab tak chodunga. jaa gaand mara.
Deletehow does dis work??
ReplyDeletewen u trace d code, d 1st queen gets placed in col 1, 2nd in col 2, 3rd in col3 n 4th in col 4- wen n=4
den hw do d solutions strt wid other nos?
i mean to say- how is it dat u find a no of solutions in dis code? u dont use recursion ryt! den hw?
plz plz plz explain,, hav a prac exam on dis!
thnx,,
horrible!!!!!!!!!
ReplyDeletetum sab harami ho sale,parhai choro madharchodon aur apni apni biwi bahan ki jor jor se mere lund se chodna suru karo.pura sperm giraunga ki wo log 100-100 bachon ki maa ban jayengi.... unke chuchiyon ko pura masal dunga....aur sara dudh usme se pi lunga....apne lund se unke muh me apna malai giraunga...
ReplyDeleteMadarchod kaun hai tu be? zyada gaali bak rha hai? Khud kar sakta hai ye?
ReplyDeleteDon't you people can talk decently rather than talking like this ? Shame on your culture.
ReplyDeletethanks friends
ReplyDeletejust add header files
thanks friends
ReplyDeletejust add header files
Header File
ReplyDelete