1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
long computeTime(Mine mines_restantes[], long production, long argent, long temps){
// Si plus de mines, le resultat est le temps courant
if(isEmpty(mines_restantes)){
return temps;
}
// Sinon, on calcule le temps minimum :
// pour chaque mine, je regarde quel est le resultat si c'est elle que je construis, et je choisis le minimum !
long temps_min[] = new long[mines_restantes.length];
for(int index = 0 ; i<mines_restantes.length ; ++i){
Mine mine = mine_restantes[i];
if(mine.cout() <= argent){
// Dans ce cas, la mine peut etre construite instantanement !
temps_min[i] = computeTime(mines_restantes.remove(i), production + mine.production(), argent - mine.cout(), temps);
}
else{
// Sinon, combien de temps pour la construire ?
long difference = mine.cout() - argent;
long modulo = difference % production;
short offset = 0;
if(modulo != 0){
offset = 1;
}
long temps_construction = difference / prodution + offset;
temps_min[i] = computeTime(mines_restantes.remove(i), production + mine.production(), argent + (temps_construction * production) - mine.cout(), temps + temps_construction);
}
}
// Retourner le temps minimum
int indexMin = getIndexMin(temps_min);
long temps_min_glob = temps_min[indexMin];
return temps_min_glob;
} |
Partager