Hello all. Quick question on an assignment I've been working on for a while now:
What this problem calls for is to solve the N-Queens problem using a stack and backtracking.
Basically, the program calls on a N size board, with N queens. The object of the game is to place N queens on a NxN size board, without any queens being on the same row, column, or diagonal.
My process is this:
1.) I push the first queen on the first column of the first row.
2.) I then try to push a second queen on the second row, and compare each position to see if there is a conflict. If there is, I move onto the next column, and so on. If there is NOT a conflict, then that position is good, and we move onto the next ROW.
3.) We do the same thing above. If, there are conflicts for every column when placing the next queen (we run out of columns), then we POP the current queen, and backtrack! Meaning we go to the previous queen, and move it again to another working spot.
4.) We continue this cycle until all N queens have been placed on the board, and no conflicts arise.
I am supposed to find all solutions. Here is my code so far:
The main method is not supposed to be changed. As you can see, if you run it, there is a StackExcception thrown, due to the peek method, but I'm not sure what I'm doing wrong. Can anyone help? If you have any more questions, please ask.Code:import java.util.Stack; //fill in your information in the line below, and comment it out in order to compile //Name: ??? Student ID: ???; public class Assignment3 { boolean conflict, complete = false; //***** fill in your code here ***** //feel free to add additional methods as necessary //finds and prints out all solutions to the n-queens problem public static int solve(int n) { //create a stack //each element stores the position of the queen on a different row Stack<Integer> s = new Stack<Integer> (); int solution = 0; int nextQueen = 0; boolean problem = false; s.push(0); do{ for(int i = 0; i < s.size(); i++) { if(s.get(i) == nextQueen){ problem = true; break; } else if(s.get(i) - i == nextQueen - s.size()){ problem = true; break; } else if(s.get(i) + i == nextQueen + s.size()){ problem = true; break; } } //***** fill in your code here ***** if(problem = false){ s.push(nextQueen); nextQueen = 0;} else{ nextQueen++; } if(nextQueen == n){ if(s.peek() == n){ s.pop(); nextQueen = s.pop()+ 1; } else{ nextQueen = s.pop()+ 1; } } }while(s.size() != n); printSolution(s); solution++; return solution; //(when a solution is found, call print_solution(s) to print it out) }//solve() //this method prints out a solution from the current stack //(you should not need to modify this method) private static void printSolution(Stack<Integer> s) { for (int i = 0; i < s.size(); i ++) { for (int j = 0; j < s.size(); j ++) { if (j == s.get(i)) System.out.print("Q "); else System.out.print("* "); }//for System.out.println(); }//for System.out.println(); }//printSolution() // ----- the main method ----- // (you shouldn't need to change this method) public static void main(String[] args) { int n = 8; // pass in parameter n from command line if (args.length == 1) { n = Integer.parseInt(args[0].trim()); if (n < 1) { System.out.println("Incorrect parameter"); System.exit(-1); }//if }//if int number = solve(n); System.out.println("There are " + number + " solutions to the " + n + "-queens problem."); }//main() }//Assignment3
XI Wiki


