public class QueenCounter { private IQueenState myBoard; private int mySize; private int myCount; public QueenCounter(int n){ mySize = n; myBoard = new QBoard(n); solve(0); System.out.println("# places = "+myCount); } /** * Queens have been placed in all columns [0..col), try to place * a queen in column col and all columns after * it, returning true if this is possible, false otherwise. * @param col is left-most column with no queen in it * @return true if a queen can be placed in all columns [col..size) */ public void solve(int col){ if (col == mySize) { myCount++; return; } // try each row until all are tried for(int r=0; r < mySize; r++){ if (myBoard.safeToPlace(r,col)){ myBoard.setQueen(r,col,true); solve(col+1); myBoard.setQueen(r,col,false); } } } public static void main(String[] args){ int size = 11; QueenCounter q = new QueenCounter(size); } }