Tuesday, 15 September 2015

c++ - How to complete this for-loop that calls a function -



c++ - How to complete this for-loop that calls a function -

i having problem trying create for-loop go on completion in 1d queens problem.

first, used goto statements everything. trying rid of goto statements using functions instead. rid of of them, focusing on nr (new row) , backtrack first, since meant phone call each other.

the loop having problem 1 checks if position safe queen. point out for-loop not finish in comments.

//forward declarations int backtrack (int board[], int& c_position); //nr: q[c]++; //if (q[c]==8) goto backtrack; void nr (int board[], int& c_position) //new row { board[c_position]++; if (board[c_position]==8) {backtrack(board, c_position);} } int backtrack (int board[], int& c_position) // backtrack { c_position--; if (c_position==-1) {system("pause"); exit(1);} nr(board, c_position); } int main () { int q[8] = {0}; //1d array, board, set 0; int c=0; int count=0; nc: c++; //new column if (c==8) goto print; q[c]=-1; nr(q, c); //test see if position safe (int i=0; i<c; i++) //this loop having problem { if ( (q[i]==q[c]) || ((c-i)==abs(q[c]-q[i])) ) { nr(q, c); } } goto nc; print: //printing 1d board gives single line, each number represents row queen count++; cout << count << endl; for(int j = 0; j <= 7; j++) { cout << q[j] << " "; } cout << endl; backtrack(q, c); system("pause"); homecoming 0; }

you're passing c reference function passes function decrements it.

that appears foil (outer goto-based) loop's effort increment it.

anyway, that's i'd @ more closely.

c++ function for-loop n-queens

No comments:

Post a Comment