Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Queens {
- public static void main(String[] args) {
- Board board = new Board(8); //create new board
- System.out.println( explore(board, 1) ); //total
- }
- public static int explore(Board board, int row) {
- int total = 0;
- //Base case: when no rows left
- if( row > board.size() ) {
- board.print();
- System.out.println();
- return 1;
- //Recursive case
- } else {
- for(int i = 1; i <= board.size(); i++) {
- if( board.safe(row, i) ) { //check if target space is free
- board.place(row, i); //place piece
- total += explore(board, row + 1); //explore rest of decision tree
- board.remove(row, i); //remove piece to backtrack and explore other branches
- }
- }
- }
- return total;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement