dumb 8 queens c++ code help
Apr 5, 2017 at 11:48pm UTC
the assignment was to write a code for dumb 8 queens where you start with all the queens in the first row then test and move queens.i wrote the code but don't know whats wrong with it.i fixed the error warning message by moving the return true but now it won't print anything.but there are no error messages. i don't know whats wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include<iostream>
#include<cmath>
using namespace std;
bool ok(int board[8][8]){
//find where the queens are
for (int c=0;c<8;c++){
int r=0;
while (board[r][c]!=1){
r++;
}
//row test
for (int i=0;i<c;i++){
if (board[r][i]==1)return false ;
}
//up diagonal test
for (int i=1;(r-i)>=0&&(c+i)>=0;i++){
if (board[r+i][c+i]==1)return false ;
}
//down diagonal test
for (int i=1; (r+i)<8 && (c+i)>=0;i++){
if (board[r+i][c-i]==1)return false ;
}
}
return true ;
}
void print(int board[8][8]){
static int count=0;
count++;
cout<<"solution #" <<count<<":" <<endl;
for (int r=0;r<8;r++){
for (int c=0;c<8;c++){
cout<<board[r][c]<<" " ;
}
cout<<endl;
}
cout<<endl;
}
int main(){
int board[8][8]={0};
for (int i0=0;i0<8;i0++){
for (int i1=0;i1<8;i1++){
for (int i2=0;i2<8;i2++){
for (int i3=0;i3<8;i3++){
for (int i4=0;i4<8;i4++){
for (int i5=0;i5<8;i5++){
for (int i6=0;i6<8;i6++){
for (int i7=0;i7<8;i7++){
board[i0][0]=1;
board[i1][1]=1;
board[i2][2]=1;
board[i3][3]=1;
board[i4][4]=1;
board[i5][5]=1;
board[i6][6]=1;
board[i7][7]=1;
if (ok(board)==true ){
print(board);
board[i0][0]=0;
board[i1][1]=0;
board[i2][2]=0;
board[i3][3]=0;
board[i4][4]=0;
board[i5][5]=0;
board[i6][6]=0;
board[i7][7]=0;
}
}
}
}
}
}
}
}
}
return 0;
}
Last edited on Apr 7, 2017 at 12:46am UTC
Apr 6, 2017 at 12:53am UTC
Do you think the unconditional return on line 23 should be inside or outside the outermost for loop?
Apr 6, 2017 at 10:21pm UTC
i moved the return true on line 23 to the outer most for loop but now the program doesn't print anything out
Apr 7, 2017 at 9:01pm UTC
I have actually already written a solution for this problem, but in a different way. If you're really stuck, feel free to ask me a question about this problem (PM).
Topic archived. No new replies allowed.