-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrossBridgeTest.cpp
194 lines (156 loc) · 5.57 KB
/
CrossBridgeTest.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <yaml-cpp/yaml.h>
#include <exception>
using namespace std;
// Add constructor and destructor
class ReadYAML{
private:
unsigned int i;
public:
unsigned int bridgesCount;
vector<float> bridgesLength;
vector<float> personSpeed;
vector<unsigned int> personPosition;
int readYaml();
ReadYAML(); //Constructor decalaration
~ReadYAML(); //destructor decalaration
};
// Member functions definitions including constructor
ReadYAML::ReadYAML(void) {
cout << "Object for ReadYAML is being created" << endl;
}
ReadYAML::~ReadYAML(void) {
cout << "Object for ReadYAML is being deleted" << endl;
}
//handle if the person list is empty or negative
//handle if the bridge count is empty or negative
//handle if the person speed is empty or negative
//handle if the person position is empty or negative
int ReadYAML::readYaml(){
YAML::Node usrInp = YAML::LoadFile("userInp.yaml"); //Load the Yaml file
try{
bridgesCount = usrInp["bridges"].as<unsigned int>();
bridgesLength = usrInp["length"].as<vector<float>>();
personSpeed = usrInp["personsSpeed"].as<vector<float>>();
personPosition = usrInp["personsPosition"].as<vector<unsigned int>>();
if(bridgesLength.empty() || personSpeed.empty() || personPosition.empty()){
cout<<"Error:Yaml file empty data"<<endl;
return 0;
}
if( (bridgesCount != bridgesLength.size()) || (personSpeed.size() != (personPosition.size()))){
cout<<"Error:User data is not Valid"<<endl;
return 0;
}
if (bridgesCount < 1){
cout<<"Error:Bridge data must be positive integers"<<endl;
return 0;
}
for(i = 0; i < bridgesLength.size(); i++){
if(bridgesLength[i] < 0){
cout<<"Error:Bridge data must be positive integers"<<endl;
return 0;
}
}
for(i = 0; i < personSpeed.size(); i++){
if((personSpeed[i] < 0) || (personPosition[i] < 0)){
cout<<"Error:Person data must be positive integers"<<endl;
return 0;
}
}
}
catch (exception& e){
cout << "Error:Yaml file reading Error: " << e.what() << endl;
return 0;
}
return 1;
}
//add constructor and destructor.
//the time taken should be variable of a class.
class CrossingBridge{
private:
float totalTimeTaken ;
public:
int status = 0;
ReadYAML objectYaml;
float getMinimumTime();
int get_minimum_crossing_time(unsigned int,vector<float> );
CrossingBridge(float time); //Created Crossing Bridge object constructor
};
// Member functions definitions including constructor
CrossingBridge::CrossingBridge( float time) {
totalTimeTaken = time;
}
//return time based on person speed read from YAML file
float CrossingBridge::getMinimumTime(){
unsigned int i,j,cnt;
status = objectYaml.readYaml();
if (status == 0 ){
return 0;
}
cnt = 0;
for(i = 0; i < objectYaml.bridgesCount; i++) {
for(j=0; j<objectYaml.personSpeed.size(); j++)
{
if(objectYaml.personPosition[j] == (i+1)){
cnt = cnt + 1;
}
}
vector<float> person_walkingTime(cnt);
//Sort the personSpped in descending Order
sort(objectYaml.personSpeed.begin(),objectYaml.personSpeed.begin()+cnt,greater<float>());
//Calculate time of each person
for(j=0;j<cnt;j++)
{
person_walkingTime[j] = objectYaml.bridgesLength[i]/objectYaml.personSpeed[j];
}
totalTimeTaken = get_minimum_crossing_time(cnt,person_walkingTime) + totalTimeTaken;
}
return totalTimeTaken;
}
//return min bridge crossing time for n person(s)
//handle special cases if n = 0, n =1 , n =2, n = 3
//handle special case if vector of person is not valid
int CrossingBridge::get_minimum_crossing_time(unsigned int n,vector<float> person_walkingTime)
{
if (n == 0)
{
return 0;
}
if (n < 3)
{
return person_walkingTime.back();
}
else if (n == 3)
{
// Person 0 and 1 cross from A to B
// Person 0 crosses back to A
// Person 0 and 2 cross from A to B
return person_walkingTime[1] + person_walkingTime[0] + person_walkingTime[2];
}
float time_taken = 0;
// Person 0 and Person 1 cross from A to B
time_taken += person_walkingTime[1];
// Person 1 crosses back to A
time_taken += person_walkingTime[1];
// Person (n-2) and Person (n-1) cross from A to B
time_taken += person_walkingTime[n-1];
// Person 0 goes back and forth to escort everyone else
for (unsigned int i = 1; i < n-2; ++i)
{
// Person 0 crosses back to A
time_taken += person_walkingTime[0];
// Person 0 and Person i cross from A to B
time_taken += person_walkingTime[i];
}
return time_taken;
}
int main(void)
{
//Create object to get the time taken to cross the bridge
CrossingBridge getTime(0.0);
float totalTimeTaken = getTime.getMinimumTime();
printf("The Minimum crossing time: %f\n",totalTimeTaken);
return 0;
}