This commit is contained in:
Victor 2024-06-22 01:08:17 +02:00
parent 7d4074bded
commit 43be2c70f5
2 changed files with 48 additions and 8 deletions

View File

@ -66,5 +66,6 @@ public class Chessboard {
} }
System.out.println(); System.out.println();
} }
System.out.println("\n\n");
} }
} }

View File

@ -4,25 +4,46 @@ public class EightQueensSolver {
private Chessboard chessboard = new Chessboard(); private Chessboard chessboard = new Chessboard();
public EightQueensSolver() { public EightQueensSolver() {
System.out.println("Début");
System.out.println(Solver());
System.out.println("Fin");
chessboard.printChessboard();
} }
public void main(String[] args){ public void main(String[] args){
// Début de la résolution par méthode simplifiée
long startTimeSimSim = System.currentTimeMillis();
System.out.println("Début SimSim");
System.out.println(SolverSim(0));
System.out.println("Fin SimSim");
long endTimeSimSim = System.currentTimeMillis();
System.out.println("Temps d'exécution SimSim: " + (endTimeSimSim - startTimeSimSim) + " ms");
chessboard.printChessboard();
chessboard = new Chessboard(); // Réinitialisation du plateau
// Début de la résolution par méthode classique
long startTimeVictor = System.currentTimeMillis();
System.out.println("Début Victor");
System.out.println(Solver(0));
System.out.println("Fin Victor");
long endTimeVictor = System.currentTimeMillis();
System.out.println("Temps d'exécution Victor: " + (endTimeVictor - startTimeVictor) + " ms");
chessboard.printChessboard();
} }
private Boolean Solver() { private Boolean Solver(int level) {
if (chessboard.getNumberOfQueen() == 8) { if (chessboard.getNumberOfQueen() == Chessboard.SIZE) {
return true; return true;
} }
for (int i = 0; i < Chessboard.SIZE * Chessboard.SIZE; i++) { for (int i = level*Chessboard.SIZE; i < Chessboard.SIZE * Chessboard.SIZE; i++) {
int x = i % Chessboard.SIZE; int x = i % Chessboard.SIZE;
int y = i / Chessboard.SIZE; int y = i / Chessboard.SIZE;
if (chessboard.VerifAccessible(x, y)) { if (chessboard.VerifAccessible(x, y)) {
chessboard.placeQueen(x, y); chessboard.placeQueen(x, y);
if (!Solver()) { if (!Solver(y)) {
chessboard.removeQueen(x, y); chessboard.removeQueen(x, y);
} else { } else {
return true; return true;
@ -32,6 +53,24 @@ public class EightQueensSolver {
return false; return false;
} }
private boolean SolverSim(int line){
if(chessboard.getNumberOfQueen() == Chessboard.SIZE){
return true;
}
for(int collumn = 0 ; collumn < Chessboard.SIZE ; collumn++){
if(chessboard.VerifAccessible(collumn, line)){
chessboard.placeQueen(collumn, line);
if(SolverSim(line+1)){
return true;
}
else{
chessboard.removeQueen(collumn, line);
}
}
}
return false;
}
public Chessboard getChessboard() { public Chessboard getChessboard() {
return chessboard; return chessboard;
} }