Thursday, October 7, 2010

N Queens problem solving using BackTracking in C Programming

ALGORITHM

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

*/

19 comments:

  1. PLZ INSERT HEADER..

    ReplyDelete
  2. it is a waste of time ....no expln is given...........

    ReplyDelete
  3. Dude this is really awesome......i really cant thank u enough ..u saved me..thumbs up..!!!!

    ReplyDelete
    Replies
    1. tereko to bacha liya lekin hame to duba diya........
      loduchand tu bhi hoshari jhaad ra hai samaj to tuje bhi nahi aaya!!!!!!!!!

      Delete
    2. gandue chutiye...

      Delete
  4. yaar iski explanation toh dedo

    ReplyDelete
  5. Replies
    1. maa 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.

      Delete
  6. how does dis work??
    wen 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,,

    ReplyDelete
  7. horrible!!!!!!!!!

    ReplyDelete
  8. tum 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...

    ReplyDelete
  9. Madarchod kaun hai tu be? zyada gaali bak rha hai? Khud kar sakta hai ye?

    ReplyDelete
  10. Don't you people can talk decently rather than talking like this ? Shame on your culture.

    ReplyDelete
  11. thanks friends
    just add header files


    ReplyDelete
  12. thanks friends
    just add header files


    ReplyDelete