N-Queens in Java using Stacks, Keeps taking the same route after backtracking -


title pretty says all. i've been working on , can't figure out way prevent happening. perhaps way store invalid placements? or how implement way 'resume' last time @ row doesn't pick same value again?

ignore while i, debugging. same prints.

import java.util.stack;  public class nqueens {  //***** fill in code here ***** //feel free add additional methods necessary  //finds , prints out solutions n-queens problem public static int solve(int n) {     //***** fill in code here *****     //scaffolding code stacks.pdf     //------------------------------------------------------------------------------------------------------------------     //  create empty stack , set current position 0     stack<integer> s = new stack<integer>();      int column = 0;     int row = 0;      int solutionscount = 0;     int = 0;     //repeat {     //loop current position last position until valid position found //current row     while (i < 5){          for(column = 0; column < n ;column++) {             //if there valid position             system.out.println("top of loop");             system.out.println("column/index = " + column + "; row is: " + row);             system.out.println("stack size = " + s.size());             system.out.println();             if (isvalid(s, column, row)) {                 s.push(column);                 //push position stack, set current position 0 // move next ro                 row++;                 column = 0;             }//if         }//for          //if there no valid position         if(!isvalid(s, column, row) || column >= n){             //if stack empty, break // stop search             if(s.size() == 0){                 break; //stop search             }//if             //else pop stack, set current position next position // backtracking previous row             else{                 s.pop();                 column++;                 row--;             }//else         }//if          //if stack has size n { // solution found         if (s.size() == n){             solutionscount++;             printsolution(s);             //pop stack, set current position next position // backtracking find next solution             s.pop();             row--;             column++;         }//if          else {         }//else     i++;     // make sure change when not bug testing 4x4     }//end loop //update following statement return number of solutions found return solutionscount;  }//solve() 

this looks homework, here pointers:

  1. you modifying column variable after for loop. obviously, intend value carry on next iteration of while. first thing when while starts again set column 0 for (column = 0; ....), overrides value. information column supposed carry 1 iteration of while next?
  2. you setting column 0 inside for loop incremented 1 column++ before next for iteration.
  3. you using column in if statement after loop. expect value of variable after for loop completes?
  4. after for loop have 2 if statements trying check same condition -- whether for loop found solution. second if clearer it. need both ifs?
  5. you putting column values stack, when pop them off stack, discard them. don't have use these values?

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -