hi
i was wonder if anyone can help me change my code from two dimensional array to one dimen. ..
and it is suppose to print all the result..why does my only print once.
thanks
Code:#include <iostream> using namespace std; #define BOARDSIZE 6 bool ok(int q[BOARDSIZE][BOARDSIZE], int row,int col); void backtrack(int q[BOARDSIZE][BOARDSIZE], int row,int col); void print(int q[BOARDSIZE][BOARDSIZE]); int main () { cout << "N Queens solution for N= " << BOARDSIZE << endl << endl; int q[BOARDSIZE][BOARDSIZE]= {0}, c, i; int cntr = 0; q[0][0] = 0; c = 0; while ( c < BOARDSIZE) { i = -1; while(i < BOARDSIZE) { i++; if(i == BOARDSIZE) { backtrack(q,i,c); continue; } if(ok(q,i,c)) break; } c++; } print(q); } bool ok(int q [BOARDSIZE][BOARDSIZE],int row, int col) { for(int i = 0; i < col; i++) { if(q[row][i] == 1) return false; } for(int i = 1; (row - i) >= 0 && (col -i) >= 0; i++) { if(q[row - i][col-i] == 1) return false; } for(int i = 1; (row + i) < BOARDSIZE && (col-i) >= 0; i++) { if(q[row+i][col-i] == 1) return false; } q[row][col] = 1; return true; } void backtrack(int q[BOARDSIZE][BOARDSIZE], int row, int col) { col--; row = 0; while(q[row][col] == 0) row++; q[row][col] = 0; } void print(int q[BOARDSIZE][BOARDSIZE]) { static int count = 0; cout << ++count << endl; int i, j; for(i = 0; i <BOARDSIZE; i++) { for(j = 0; j < BOARDSIZE; j++) cout << q[i][j] << " "; cout << endl; } cout << endl; }




Reply With Quote