Note that there are some explanatory texts on larger screens.

plurals
  1. POEight Queens C++ utilizing a Stack Backtracking
    primarykey
    data
    text
    <p>So I'm doing this for homework, and I can't figure out where my bug is. Any help is greatly appreciate.</p> <p>My understanding is this.</p> <ol> <li>Initialize a stack for tracking which row &amp; column has a queen in it.</li> <li>Place a queen on the first square, push its location onto the stack. Push (0,0); Then set a variable that the row has been filled. filled+;</li> </ol> <p>3.Then Loop</p> <p>check if the current row or column have a conflict with another queen.</p> <p>a. no conflict. push to stack. increase the filled row variable. filled++; move up a row.</p> <p>b. there is a conflict. Move right. col++;</p> <p>c. cant move right anymore. pop the stack and set to row and col. subtract the filled. then move over. col++; and try again.</p> <hr> <pre><code>int main(){ bool board[8][8]; for(int i = 0; i &lt; 8; i++){ for(int j = 0; j &lt; 8; j++){ board[i][j] = false;}} int row = 0, col = 0, filled = 0; StackLi&lt;int&gt; rowStack; StackLi&lt;int&gt; colStack; rowStack.push(row); colStack.push(col); board[row][col] = true; //cout &lt;&lt; "push: " &lt;&lt; "(" &lt;&lt; row &lt;&lt; "," &lt;&lt; col &lt;&lt; ")" &lt;&lt; endl; row++; while(filled &lt; 7) { if(!isSafe(board,row,col) ) { filled++; rowStack.push(row); colStack.push(col); board[row][col] = true; //cout &lt;&lt; "push: " &lt;&lt; "(" &lt;&lt; row &lt;&lt; "," &lt;&lt; col &lt;&lt; ")" &lt;&lt; endl; if(filled &gt; 8) { print(board); return 0; } row++; } else{ col++; //cout &lt;&lt; "move: " &lt;&lt; "(" &lt;&lt; row &lt;&lt; "," &lt;&lt; col &lt;&lt; ")" &lt;&lt; endl; } if(col &gt; 7) { row = rowStack.topAndPop(); col = colStack.topAndPop(); board[row][col] = false; cout &lt;&lt; "pop: " &lt;&lt; "(" &lt;&lt; row &lt;&lt; "," &lt;&lt; col &lt;&lt; ")" &lt;&lt; endl; filled--; } } return 0; } bool isSafe(bool board[8][8], int row, int col) { for(int i = 0; i &lt; 8; i++) { if(board[row][i] || board[i][col]) return true; } for(int i = 0; (row - i)&gt;=0 &amp;&amp; (col-i) &gt;= 0; i++) { if(board[row-i][col-i]) return true; } for(int i = 0; (row - i)&lt;=8 &amp;&amp; (col-i) &gt;= 0; i++) { if(board[row+i][col+i]) return true; } return false; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload