The Eight Queens Problem is a classic chess puzzle that involves placing eight queens on a chessboard in such a way that no two queens threaten each other. This means that no two queens can share the same row, column, or diagonal.
The challenge is to find all possible solutions to this problem.
publicclassEightQueens { intmax=8; //Define an array to store the positions where the queens are placed. int[] array = newint[max]; //count the number of methods staticintcount=0;
publicstaticvoidmain(String[] args) { EightQueenseightQueens=newEightQueens(); eightQueens.place(0); System.out.println("There are " + count + " kinds of methods to solve the eight queens problem."); }
/** * Place chess pieces in a recursive manner. * * @param n the nth queen */ publicvoidplace(int n) { if (n == max) { printResult(); count++; return; }
for (inti=0; i < max; i++) { array[n] = i; if (ifNotConflict(n)) { place(n + 1); } } }
/** * Check whether the newly placed chess piece conflicts with the previous ones. * * @param n the nth queen * @return if the queen could be placed at this place */ publicbooleanifNotConflict(int n) { for (inti=0; i < n; i++) { if (array[i] == array[n] || // on the same coloum Math.abs(n - i) == Math.abs(array[n] - array[i])) { // on the same diagonal returnfalse; } } returntrue; }
publicvoidprintResult() { for (inti=0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); }