fork download
  1. import java.util.Arrays;
  2.  
  3. class NQueens {
  4. static int N, count;
  5. static int[] column;
  6.  
  7. public static void main (String[] args) {
  8. N = 14;
  9. column = new int[N];
  10. nQueens(0);
  11. System.out.println(count);
  12. }
  13.  
  14. private static void nQueens(int row) {
  15. if (row == N) {
  16. // System.out.println(Arrays.toString(column));
  17. count++;
  18. return;
  19. }
  20. for (int col = 0; col < N; col++) {
  21. if (place(row, col)) {
  22. column[row] = col;
  23. nQueens(row + 1);
  24. }
  25. }
  26. }
  27.  
  28. private static boolean place(int row, int col) {
  29. for (int i = 0; i < row; i++) {
  30. if (column[i] == col || Math.abs(column[i] - col) == Math.abs(i - row)) {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. }
Time limit exceeded #stdin #stdout 15s 320576KB
stdin
Standard input is empty
stdout
Standard output is empty