-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.c
122 lines (102 loc) · 3.43 KB
/
scheduler.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DAYS_IN_MONTH 29 // number of days we process
#define EMPLOYEE_COUNT 12 // number of employees
#define WORKING_DAYS ((int)( DAYS_IN_MONTH * 0.8 )) // maximum number of days an employee work
#define DESK_COUNT ((int)( EMPLOYEE_COUNT * 0.8 )) // number of employee working simultaneously
typedef int bool;
#define true 1
#define false 0
bool* schedule_table;
void print_table(int current_day, int current_employee) {
int day,employee;
printf("--------------- day: %d , employee: %d ----------\n", current_day, current_employee);
printf(" ");
for (employee = 0; employee < EMPLOYEE_COUNT; employee++) {
printf("%s ", employee==current_employee?"|":" ");
}
printf("\n");
for (day = 0; day < DAYS_IN_MONTH; day++) {
printf("Day %2d: ", day+1);
for (employee = 0; employee < EMPLOYEE_COUNT; employee++) {
printf("%s ", schedule_table[day*EMPLOYEE_COUNT+employee]?"x":"-");
}
if (day==current_day) {
printf("%s", "<-");
}
printf("\n");
}
printf("--------------------------------------\n");
}
bool is_employee_full(int employee){
int day,sum=0;
for (day = 0; day < DAYS_IN_MONTH; day++) {
if (schedule_table[day*EMPLOYEE_COUNT+employee]){
sum++;
}
}
return sum >= WORKING_DAYS;
}
bool is_day_full(int day){
int employee,sum=0;
for (employee = 0; employee < EMPLOYEE_COUNT; employee++) {
if (schedule_table[day*EMPLOYEE_COUNT+employee]){
sum++;
}
}
return sum >= DESK_COUNT;
}
bool is_working(int day, int employee){
return schedule_table[day*EMPLOYEE_COUNT+employee];
}
void set_working(int day, int employee){
schedule_table[day*EMPLOYEE_COUNT+employee] = true;
}
void clear_employee(int until_day, int employee){
int day;
for (day = 0; day < until_day; day++) {
schedule_table[day*EMPLOYEE_COUNT+employee] = false;
}
}
int main() {
int day = 0;
int employee = 0;
int steps = 0;
printf("\nDays: %d, working days per employee: %d, number of employees: %d, simultaneously working employees: %d\n\n", DAYS_IN_MONTH, WORKING_DAYS, EMPLOYEE_COUNT, DESK_COUNT);
schedule_table = (bool*) malloc( DAYS_IN_MONTH * EMPLOYEE_COUNT * sizeof(bool) );
do {
//print_table(day,employee);
//getchar();
steps++;
if (!is_working(day,employee) && !is_employee_full(employee)){ // only set working, if not already full
set_working(day,employee);
}
if (is_day_full(day)) {
day++; // skip the remaining employees, move to the next day
employee=0;
continue;
}
employee++; // move to the next employee
if (employee>=EMPLOYEE_COUNT){ // if all employee examined (but the desks still isn't full)
if (day==0) { // if this occurred on the first day, give up
break; // because this means the scheduler impossible without employee overtime
}
employee=0;
while (is_working(day,employee)){ // find the first, who not working on that day
employee++;
}
set_working(day,employee); // and set working
clear_employee(day,employee); // clear it's previous days
day=0; // restart from the first day from the currently changed employee
}
} while(day<DAYS_IN_MONTH);
if (day<DAYS_IN_MONTH) {
printf("\nSorry, but it's impossible to accomplish this schedule with the given parameters!\n\n");
return 1;
} else {
print_table(day,employee);
printf("\nSteps taken: %d (bruteforce would be: %.0lf)\n\n", steps, pow(2,DAYS_IN_MONTH*EMPLOYEE_COUNT));
return 0;
}
}