java - NQueens with Stacks -


my assignment find number of solutions nqueens problem n. i'm having real tough time doing n other 5. honestly, don't know problem code anymore , it's due in 5 hours. really love help/advice. (my teacher wrote main method , printsolution method , i'm allowed use stacks)

my code:

import java.util.stack;  public class nqueens {  public static int solutions = 0; public static stack<integer> queens = new stack<integer>(); public static int col = 0; public static int row = 0; public static boolean done = false;  public static boolean check(int n) {     int rd = 1;     (int = row - 1; >= 0; i--) {         if (queens.get(i) == col + rd++) return false;     } //checks diagonally right     system.out.println("right " + "col: " + col + " row: " + row);      int ld = 1;     (int = row - 1; >= 0; i--) {         if (queens.get(i) == col - ld++) return false;     } //checks diagonally left     system.out.println("left " + "col: " + col + " row: " + row);      (int = row - 1; >= 0; i--) {         if (queens.get(i) == col) return false;     } //checks vertically     system.out.println("vert " + "col: " + col + " row: " + row);     return true; }  public static void place(int n) {     if (check(n)) {         system.out.println("items in stack: " + queens.size());         queens.push(col);         system.out.println("place push " + "col: " + col);         col = 0;         row++;         //pushes queen if not interfere previous queens     }else if (col < n) {         system.out.println("place redo " + "col: " + col);         col++;         //moves next column long not out of bounds         if (col == n) done = true;         //if column reaches boundary, there no more solutions     }else {         queens.pop();         system.out.println("place pop " + "col: " + col);         col = 0;         row--;         //no possible spots in row, backtrack     } }  //finds , prints out solutions n-queens problem public static int solve(int n) {     if (queens.size() == n) {         solutions++;         printsolution(queens);         col = 0;         row--;     }//checks if program has found solution     while (!done) {         system.out.println("solve");         place(n);         return solve(n);     }//as long solutions left, keeps running     return solutions;     //all possible solutions found, return solutions }  //this method prints out solution current stack private static void printsolution(stack<integer> s) {     (int = 0; < s.size(); ++) {         (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()  // ----- main method ----- // (you shouldn't need change method) public static void main(string[] args) {      int n = 8;      // pass in parameter n 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 " + number + " solutions " + n + "-queens problem."); }//main() 

}

check link can inspiration java solution.

http://rosettacode.org/wiki/n-queens_problem#java


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -