import java.util.LinkedList;
public class EightQueens {
private static boolean isCorrectColumn(int col, int curRow, int queueCount, int[] path) {
boolean bCorrect = true;
int leftup = col - 1;
int rightup = col + 1;
for (int i = curRow - 1; i >= 0; --i) {
if (path[i] == col || path[i] == leftup || path[i] == rightup) {
bCorrect = false;
break;
}
--leftup;
++rightup;
}
return bCorrect;
}
public static void findQueuePos(int queueCount, int curRow, int[] path, LinkedList<int[]> solutions) {
if (queueCount == curRow) {
solutions.add(path.clone());
return;
}
for (int i = 0; i < queueCount; ++i) {
if (isCorrectColumn(i, curRow, queueCount, path)) {
path[curRow] = i;
findQueuePos(queueCount, curRow + 1, path, solutions);
}
}
}
public static void main(String[] args) {
int queueCount = 8;
int[] path = new int[queueCount];
LinkedList<int[]> solutions = new LinkedList<>();
findQueuePos(queueCount, 0, path, solutions);
for (int[] solution : solutions) {
for (int col : solution) {
System.out.print(col + " ");
}
System.out.println();
}
}
}