示例#1
0
  static boolean solve(int numQueens, int whichCol) {
    // Bottom-out condition 1:
    if (numQueens == 0) {
      // None to assign - done.
      return true;
    }

    // Bottom-out condition 2:
    if (whichCol >= board.size()) {
      // No columns left to try: done.
      return false;
    }

    // Try every un-forbidden spot in each row.
    for (int row = 0; row < board.size(); row++) {
      if (!board.isForbidden(row, whichCol)) {

        // Try this location.
        board.addQueen(row, whichCol);

        boolean solutionExists = solve(numQueens - 1, whichCol + 1);

        if (solutionExists) {
          return true;
        }

        // Else, un-do
        board.removeQueen(row, whichCol);
      }
    }

    // Couldn't find a solution.
    return false;
  }