Note that there are some explanatory texts on larger screens.

plurals
  1. PO8 queens problem using backtracking recurison
    primarykey
    data
    text
    <p>I've been working on the 8 queens problem but I got stuck. I don't want code. I would love guidance and directions in order to understand how to solve this problem myself using backtracking recursion.</p> <p>The program should enumerate all solutions to the N-queens problem by drawing the location of the queens in ASCII like the two solutions <a href="http://img197.imageshack.us/img197/2595/8queens.jpg" rel="nofollow">here</a>. </p> <p>My pseudocode so far is: </p> <pre><code>void queen(int n){ for( int i = 0; i &lt; n; i++){ place queen[ i ] on row i; for(int j = 0 ; j &lt; n ; j++){ if( queen[ i ] is not in the same column as queen[0] through queen[ i - 1 ] &amp;&amp; queen[ i ] is not on the same major diagonal with queen[0] through queen[ i -1 ] &amp;&amp; queen[ i ] is not on the same minor diagonal with queen[0] through queen[ i -1 ] ) { print 'Q '; } else{ print '* '; } System.out.println(); } System.out.println(); } } </code></pre> <p>There is no any backtracking recursion in my pseudocode because I don't know how to do it.</p> <p>Any help is greatly appreciated.No code, please.</p> <p>(Update in response to Nemo):</p> <pre><code>solver(int n, Board b){ for(int i = 0; i &lt; b.length; i++){ place queen in column i; for(int j = 0; j &lt; b.length; j++){ change b; solver(n+1,changed b); } } } </code></pre> <p>Is it correct? </p> <p>(Update 2):</p> <pre><code> solver8(board /* with queens presented in first 7 columns */){ // place a queen in the 8th column; for(each possible placement of the queen in column 8 or in other words int i = 0; i &lt; board.length; i++ ){ place the queen and print the board } } solver7(board /* with queens presented in first 6 columns */){ // place a queen in the 7th column; for(each possible placement of the queen in column 7 or in other words int i = 0; i &lt; board.length; i++ ){ solver8(board with queens placed in first 7 columns); } } solver6(board /* with queens presented in first 5 columns */ ){ // place a queen in the 6th column; for(each possible placement of the queen in column 6 or in other words int i = 0; i &lt; board.length; i++ ){ solver7(board with queens presented in first 6 columns); } } </code></pre> <p>and so on until</p> <pre><code> solver1(1, empty board){ for(int i = 0; i &lt; board.length; i++){ place queen in row[i] of column 1; solver2(board with queen in row[i] of column 1); } } </code></pre> <p>Update 3 (Edited):</p> <pre><code>private int numberOfQueens = 8; solver(int n, Board b){ for(int r = 0; r &lt; b.length; r++){ place queen in row[r] of column[n]; if(n == numberOfQueens){ print the board; return; } else{ solver(n+1, board with queen in row[r] of column[n]); } } } } </code></pre>
    singulars
    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.
 

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