In Java:
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. No two queens may be on the same row, column, nor diagonal.
The following image shows one possible solution for the 8-queens problem.
https://imgur.com/spcGSfj
The following are minimum requirements:
################# # # # #X# # # # # # # # # # # #X# # # # #X# # # # # # # # # # # # # #X# # #X# # # # # # # # # # # #X# # # # #X# # # # # # # # # # # # # #X# # # #################
import java.util.Scanner;
public class Queens {
public static boolean safe(int[] mat, int n) {
for (int i = 0; i < n; i++) {
if (mat[i] == mat[n])
return false;
if ((mat[i] - mat[n]) == (n - i))
return false;
if ((mat[n] - mat[i]) == (n - i))
return false;
}
return true;
}
//this function prints the queens
public static void print(int[] mat) {
int n = mat.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (mat[i] == j)
System.out.print("X ");
else
System.out.print("# ");
}
System.out.println();
}
System.out.println();
}
//this function does the backtracking and all the possible
permutations are tried
public static void solution(int n) {
int[] arr = new int[n];
solution(arr, 0);
}
public static void solution(int[] mat, int k) {
int n = mat.length;
if (k == n)
print(mat);
else {
for (int i = 0; i < n; i++) {
mat[k] = i;
if (safe(mat, k))
solution(mat, k+1);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value of N(must be greater than 3): ");
int n = sc.nextInt();
solution(n);
}
}


In Java: The n-queens puzzle is the problem of placing n queens on an n×n chessboard...