-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedwork.cpp
139 lines (113 loc) · 3.8 KB
/
schedwork.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <set>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
// add or remove necessary headers as you please
#include "schedwork.h"
using namespace std;
// a constant that can be used to indicate an INVALID
// worker ID if that is useful to your implementation.
// Feel free to not use or delete.
static const Worker_T INVALID_ID = (unsigned int)-1;
// Add prototypes for any helper functions here
bool isValid(const AvailabilityMatrix& avail, Worker_T worker, const size_t maxShifts,
std::vector<std::vector<Worker_T> > dailySchedule,
unsigned int day, int shift_count[]
);
bool scheduleHelper(
const AvailabilityMatrix& avail,
std::vector<std::vector<Worker_T> > dailySchedule,
unsigned int day, const size_t dailyNeed, int shift_count[],
unsigned int spot, const size_t maxShifts);
// Add your implementation of schedule() and other helper functions here
bool schedule(
const AvailabilityMatrix& avail, //k
const size_t dailyNeed, //d
const size_t maxShifts, //m
DailySchedule& sched
)
{
if(avail.size() == 0U){
return false;
}
sched.clear();
std::vector<std::vector<Worker_T> > dailySchedule;
//create schedule vector
vector<Worker_T> empties;
for(int i = 0; i< (int)avail.size(); ++i){
dailySchedule.push_back(empties);
}
//create shift_count
int size = avail[0].size();
int shift_count[size];
for(int i = 0; i<size; ++i){ //create shift_count vector
shift_count[i] = 0;
}
//initialize vars
unsigned int day = 0;
unsigned int spot = 0;
//call helper function
bool valid = scheduleHelper(avail, dailySchedule,day++,
dailyNeed, shift_count, spot, maxShifts);
return valid;
}
bool isValid(const AvailabilityMatrix& avail, Worker_T worker, const size_t maxShifts,
std::vector<std::vector<Worker_T> > dailySchedule,
unsigned int day, int shift_count[]
){
if(avail[day][worker] != true && // check worker available
shift_count[worker] > (int) maxShifts) //check if reach max shifts
{
return false;
}
//loop thru schedule
for(int i = 0; i < (int) dailySchedule[day].size();i++) {
if(dailySchedule[day][i] == worker){
return false;
}
}
return true;
}
bool scheduleHelper(
const AvailabilityMatrix& avail,
std::vector<std::vector<Worker_T> > dailySchedule,
unsigned int day, const size_t dailyNeed, int shift_count[],
unsigned int spot, const size_t maxShifts
){
if(day == avail.size()-1){ //if filled all spots, return
return true;
}
for(int i = 0; i < (int) avail[0].size(); i++){ //loop thru workers
if(isValid(avail,i,maxShifts, dailySchedule,day, shift_count)){ //if meet constraints
shift_count[i]++; //increase # of shifts
dailySchedule[day].push_back(i); //add worker
if(spot == dailyNeed){ //reach end of day
if(scheduleHelper(avail, dailySchedule,day++,
dailyNeed, shift_count, spot, maxShifts)){ //recurse to next day
return true;
}
else{
//undo changes if not return true
day--;
shift_count[i]--;
dailySchedule[day].pop_back();
}
}
else{
if(scheduleHelper(avail, dailySchedule,day,
dailyNeed, shift_count, spot++, maxShifts)){ //recurse next spot
return true;
}
else{
//undo changes if not return true
shift_count[i]--;
dailySchedule[day].pop_back();
}
}
}
} //for loop
return false;
}