From 43be2c70f557a0c03e58eeee3c22c2e7b63c5692 Mon Sep 17 00:00:00 2001 From: Victor Date: Sat, 22 Jun 2024 01:08:17 +0200 Subject: [PATCH] changes --- .../fr/iut_fbleau/but3/dev6_2/Chessboard.java | 1 + .../but3/dev6_2/EightQueensSolver.java | 55 ++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java b/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java index cb8d70d..ae9e29b 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java +++ b/src/main/java/fr/iut_fbleau/but3/dev6_2/Chessboard.java @@ -66,5 +66,6 @@ public class Chessboard { } System.out.println(); } + System.out.println("\n\n"); } } diff --git a/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java b/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java index a5e2690..70f7f56 100644 --- a/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java +++ b/src/main/java/fr/iut_fbleau/but3/dev6_2/EightQueensSolver.java @@ -4,25 +4,46 @@ public class EightQueensSolver { private Chessboard chessboard = new Chessboard(); public EightQueensSolver() { - System.out.println("Début"); - System.out.println(Solver()); - System.out.println("Fin"); - chessboard.printChessboard(); + } + + + 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() { - if (chessboard.getNumberOfQueen() == 8) { + private Boolean Solver(int level) { + if (chessboard.getNumberOfQueen() == Chessboard.SIZE) { 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 y = i / Chessboard.SIZE; if (chessboard.VerifAccessible(x, y)) { chessboard.placeQueen(x, y); - if (!Solver()) { + if (!Solver(y)) { chessboard.removeQueen(x, y); } else { return true; @@ -32,6 +53,24 @@ public class EightQueensSolver { 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() { return chessboard; }