A small workspace repository from the second-year Java course. It contains one complete algorithmic exercise, the eight queens problem, solved by backtracking.
| Author | Ștefan Horia-Eusebiu |
| Language | Java 17 (Maven project, no external dependencies) |
| Year | 2024 |
OptRegine.java places eight queens on a chessboard so that
none of them attacks any other. The board is an int[8][8] matrix and the search proceeds
column by column:
private boolean plaseazaRegine(int[][] tabla, int coloana) {
if (coloana >= N) return true; // all columns filled → solution found
for (int i = 0; i < N; i++) {
if (esteSigur(tabla, i, coloana)) {
tabla[i][coloana] = 1; // place
if (plaseazaRegine(tabla, coloana + 1)) return true;
tabla[i][coloana] = 0; // backtrack
}
}
return false;
}Because exactly one queen is placed per column, esteSigur() only has to check three directions to
the left — the row, the upper-left diagonal and the lower-left diagonal. Everything to the right is
still empty, so it needs no checking at all. That observation is what keeps the safety test at
O(n) instead of O(n²).
The solution is printed as a text board, with R for a queen and . for an empty square.
Known issue: in
afiseazaTabla(), a queen prints as"R"while an empty square prints as". ", so the columns do not line up. Printing"R "fixes the alignment.
mvn compile
mvn exec:java -Dexec.mainClass="javaAn2.OptRegine"Or open the project in IntelliJ IDEA and run OptRegine directly.
Main.java is the default class generated by the IDE and simply prints a greeting followed by the
numbers 1 to 5; it is not part of the exercise.
Academic work published for portfolio purposes.