ajout TP1 presque finis

This commit is contained in:
James Boutaric
2025-09-11 11:37:24 +02:00
parent 001ee76228
commit bf9189502a
10 changed files with 246 additions and 44 deletions

43
TP1/MinimaxMemo.java Normal file
View File

@@ -0,0 +1,43 @@
public class MinimaxMemo {
// Minimax avec mémo (on évite de recalculer les mêmes positions)
public static Integer[] memoMax;
public static Integer[] memoMin;
public static int exploreMax(int n) {
if (n <= 0) return -1;
if (memoMax[n] != null) return memoMax[n];
int meilleur = Integer.MIN_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMin(n - coups);
meilleur = Math.max(meilleur, res);
}
memoMax[n] = meilleur;
return meilleur;
}
public static int exploreMin(int n) {
if (n <= 0) return +1;
if (memoMin[n] != null) return memoMin[n];
int pire = Integer.MAX_VALUE;
for (int coups = 1; coups <= 3; coups++) {
int res = exploreMax(n - coups);
pire = Math.min(pire, res);
}
memoMin[n] = pire;
return pire;
}
public static void main(String[] args) {
int n = 25; // taille du tas initial
memoMax = new Integer[n + 1];
memoMin = new Integer[n + 1];
int resultat = exploreMax(n);
System.out.println("Résultat pour n=" + n + "" + resultat);
}
}