Dumb 8 Queens problem
875311Jul 13 2011 — edited Jul 13 2011this is hw #2. i am having problems with this one. the professor gave us an outline of the program which is:
//dumb 8 queens using a 2-dimensional array to represent the board
#include<cmath>
#include<iostream>
using namespace std;
bool ok( … what goes here …){
… and here …
};
void print( … what goes here … ){
… and here …
}
; int main( ) {
int board[8][8]={0};
int count = 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 ++){
// use the indices of the loops to set a configuration in array board
…
// if this configuration is conflict-free, print the count and the board
if(ok(board)) print(board, ++count);
// reset the board for the next configuration
…
}
return 0;
}
PROFESSOR ALSO GAVE US A PAGE THAT HAS HINTS:
//dumb 8 queens using a 2-dimensional array to represent the board
#include<cmath> #include<iostream> using namespace std;
bool ok( int b[][8]){
int i;
for (int col=7; col>0; col--){ // go through all the columns
// for each column, going right to left
// find the row that the queen is on (using the while loop) and then
// check that position for validity to the left
// step1: first find the row in this column
// step2: now test this position against all the columns to the left
Do this using the code for the 3 tests we did in class:
1. row test
2. up-diagonal test
3. down-diagonal test
but if as you search you find a “1”, return false
} // end for
return true;
}// end ok function
void print(int b[][8], int count){
as discussed in class
};
//see next page
int main( ){ int board[8][8]={0}; int count = 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 ++){
// use the indices of the loops to set a configuration in array board
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 this configuration is conflict-free, print the count and the board
if(ok(board)) print(board, ++count);
// reset the board for the next configuration
undo the above assignments to “clear the board (with “0”) so as to be able to consider the next configuration
} return 0; }
HOW DO I BEGIN THIS ASSIGNMENT? IS IT THE SAME AS HW#1 OR WHAT? I AM CONFUSED.