Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. public class Queens {
  2.  
  3. public static void main(String[] args) {
  4. Board board = new Board(8); //create new board
  5. System.out.println( explore(board, 1) ); //total
  6.  
  7. }
  8.  
  9. public static int explore(Board board, int row) {
  10. int total = 0;
  11. //Base case: when no rows left
  12. if( row > board.size() ) {
  13. board.print();
  14. System.out.println();
  15. return 1;
  16. //Recursive case
  17. } else {
  18. for(int i = 1; i <= board.size(); i++) {
  19. if( board.safe(row, i) ) { //check if target space is free
  20. board.place(row, i); //place piece
  21. total += explore(board, row + 1); //explore rest of decision tree
  22. board.remove(row, i); //remove piece to backtrack and explore other branches
  23. }
  24. }
  25.  
  26. }
  27. return total;
  28.  
  29. }
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement