Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// Global constants.
// Your code should be written in such a way that it will
// continue to work properly if these values are changed.
const int minFoodPerDrop = 40, // min food per drop
maxFoodPerDrop = 60, // max food per drop
requiredFoodPerVillage = 100, // food each village must have
maxVillages = 10, // max number of villages
tableEntries = 18, // number of table entries
landedInSea = -1; // special value used to represent "landed in sea"
// Simulates a single relief campaign and returns the number of drops
// required to supply all of the villages.
int simulateCampaign (int numberOfVillages) {
int unitsReceived[maxVillages] = { 0 }, drops = 0,
targetVillage, landingVillage;
for (;;) {
// Decide where the next drop should be targeted.
targetVillage = pickTargetVillage (numberOfVillages, unitsReceived);
if (unitsReceived[targetVillage] >= requiredFoodPerVillage) {
// The village with the least amount of food has enough.
// The campaign is complete. Return the number of drops required.
return drops;
}
// Work out where the drop will acually land.
landingVillage = pickLandingVillage (numberOfVillages, targetVillage);
if (landingVillage != landedInSea) {
// The drop did not go into the sea.
// Update the food received by the village where it landed.
unitsReceived[landingVillage] += pickUsableUnits();
}
drops++;
}
}