diff --git a/rotagen.c b/rotagen.c index 8ff073b..06a556e 100644 --- a/rotagen.c +++ b/rotagen.c @@ -83,23 +83,27 @@ int main(void) void generate_rota(struct slot_result *rota_out) { + int prev[MAX_JOBS]; + for (int job = 0; job < num_jobs; ++job) + prev[job] = num_people; + for (int slot = 0; slot < num_slots; ++slot) { do { - for (int j = 0; j < num_jobs; ++j) { - struct allocation *allocation = &rota_out[slot].allocations[j]; + for (int job = 0; job < num_jobs; ++job) { + struct allocation *allocation + = &rota_out[slot].allocations[job]; do { - generate_allocation(slot, j, allocation); - } while (!satisfies_allocation_constraints(allocation)); + allocation->slot = slot; + allocation->job = job; + allocation->person = rand() % num_people; + } while (!satisfies_allocation_constraints(allocation) + || previously_allocated(prev, allocation->person)); } } while (!satisfies_slot_constraints(&rota_out[slot])); - } -} -void generate_allocation(int slot, int job, struct allocation *allocation_out) -{ - allocation_out->slot = slot; - allocation_out->job = job; - allocation_out->person = rand() % num_people; + for (int job = 0; job < num_jobs; ++job) + prev[job] = rota_out[slot].allocations[job].person; + } } bool satisfies_allocation_constraints(const struct allocation *allocation) @@ -127,6 +131,15 @@ bool satisfies_allocation_constraints(const struct allocation *allocation) return true; } +bool previously_allocated(int prev[MAX_JOBS], int person) +{ + for (int job = 0; job < num_jobs; ++job) { + if (prev[job] == person) + return true; + } + return false; +} + bool satisfies_slot_constraints(const struct slot_result *result) { for (int i = 0; i < num_jobs; ++i) { diff --git a/rotagen.h b/rotagen.h index 863c731..ff09fe3 100644 --- a/rotagen.h +++ b/rotagen.h @@ -32,6 +32,7 @@ struct slot_result { void generate_rota(struct slot_result *rota_out); void generate_allocation(int slot, int job, struct allocation *allocation_out); bool satisfies_allocation_constraints(const struct allocation *allocation); +bool previously_allocated(int previous_allocations[MAX_JOBS], int person); bool satisfies_slot_constraints(const struct slot_result *slot); void print_rota(const struct slot_result *rota);