44 lines
1.2 KiB
Java
44 lines
1.2 KiB
Java
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);
|
|
}
|
|
}
|