I just recently(as in a few hours ago) decided to take up a small program my dad put up for me. It's the N-Queens Problem(Google it).
The code is below. Now I know that the code is pretty redundant but I honestly don't care too much about that.
Now I'm not getting any compile errors but when I try to run the program, it just blinks out and then comes back to the same screen. Try running this in your C++ Compiler. I don't get an output. I'm not getting any errors either.Code:#include <iostream.h> #include <conio.h> #include <math.h> int b[8][8]; void setzero(int x)//This sets the remaining non-conflicted spaces in the board to zero. x is the row number. { int v,w; for (v=x;v<=8;v++) { for (w=1;w<=8;w++) { if(b[v][w]>=x) b[v][w]=0; } } } void options(int y,int z)//This function is just to remove the places on the board which are crossed out by any one queen. { int d,e,f,g; for (d=1;d<=8;d++)//Getting the row. if (b[d][z]!=9) if (b[d][z]==0) b[d][z]=y; for (e=1;e<=8;e++)//Getting the Column. if (b[y][e]!=9) if (b[y][e]==0) b[y][e]=y; for (f=1;f<=8;f++)//For the diagonals. for (g=1;g<=8;g++) { if ((f+g)==(y+z)) {if (b[f][g]==0) b[f][g]=y; } else if(fabs(f-g)==fabs(z-y)) { if (b[f][g]==0) b[f][g]=y; } }} void main() { int i,j,k,l,m,n,o,p,q,r,s,t,u,v,w; //Declaring a buttload of variables. for (i=1;i<=8;i++)//Row 1 { int ctr = 1; setzero(ctr); b[1][i]=9; options(1,i); for(j=1;j<=8;j++)//Row 2 { ctr = 2; setzero(ctr); if (b[2][j] == 0) { b[2][j]=9; options(2,j); for(k=1;k<=8;k++)//Row 3 { ctr = 3; setzero(ctr); if(b[3][k]==0) { b[3][k]=9; options(3,k); for(l=1;l<=8;l++)//Row 4 { ctr = 4; setzero(ctr); if (b[4][l]==0) { b[4][l]=9; options(4,l); for(m=1;m<=8;m++)//Row 5 { ctr = 5; setzero(ctr); if (b[5][m]==0) { b[5][m]=9; options(5,m); for(n=1;n<=8;n++)//Row 6 { ctr = 6; setzero(ctr); if (b[6][n]==0) { b[6][n]=9; options(6,n); for(o=1;o<=8;o++)//Row 7 { ctr = 7; setzero(ctr); if (b[7][o]==0) { b[7][o]=9; options(7,o); for(p=1;p<=8;p++)//Row 8 { ctr = 8; setzero(ctr); if (b[8][p]==0) { b[8][p] = 9; s=0; for(q=1;q<=8;q++)//To check whether i got a solution at all or not. Solution means 8 queens should be there on the board. S is the number of queens. { for(r=1;r<=8;r++) { if (b[q][r]==9) s=s+1; } } if (s == 8) { for (t=1;t<=8;t++)//If 8 queens are there, this prints the column number in each row seperated with a '-'. for (u=1;u<=8;u++) if (b[t][u]==9) cout<<u<<"-"; }}}}}}}}}}}}}}}}}
EDIT: Code edited.
Any ideas?



10Likes
LinkBack URL
About LinkBacks




ptions() respectively.