-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparking.cpp
281 lines (266 loc) · 7.88 KB
/
parking.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
using namespace std;
// enum type for Vehicle
enum class VehicleSize {
Motorcycle,
Compact,
Large
};
class Vehicle {
// Write your code here
protected:
VehicleSize size;
VehicleSize spot;
int ind;
int lvl;
public:
VehicleSize get_size(){return size;}
VehicleSize spot_size(){return spot;}
int get_spot(){return ind;}
int get_level(){return lvl;}
void park_vehicle(int level,int pos,VehicleSize s){
ind = pos;
lvl = level;
spot = s;
}
};
class Bus: public Vehicle {
// Write your code here
public:
Bus(){
size = VehicleSize::Large;
lvl = -1;
ind = -1;
}
};
class Car: public Vehicle {
// Write your code here
public:
Car(){
size = VehicleSize::Compact;
lvl = -1;
ind = -1;
}
};
class Motorcycle: public Vehicle {
// Write your code here
public:
Motorcycle(){
size = VehicleSize::Motorcycle;
ind = -1;
lvl = -1;
}
};
class Level {
// Write your code here
private:
int rows,spots_row;
public:
vector<vector<int> > motorspots;
vector<vector<int> > carspots;
vector<vector<int> > busspots;
Level(int numrows,int spots_per_row){
rows = numrows;
spots_row = spots_per_row;
motorspots.resize(numrows);
carspots.resize(numrows);
busspots.resize(numrows);
int motor_size = spots_per_row/4;
int car_size = 2*motor_size;
int bus_size = spots_per_row - 3*motor_size;
for(auto& m:motorspots)m = vector<int>(motor_size,0);
for(auto& c:carspots)c = vector<int>(car_size,0);
for(auto& b:busspots)b = vector<int>(bus_size,0);
}
int canpark(Vehicle &v,VehicleSize spot,int num){
if(spot==VehicleSize::Motorcycle){
for(int i = 0;i < rows;i++){
for(int j = 0;j < motorspots[i].size();j++){
if(motorspots[i][j]==0)return i*motorspots[i].size()+j;
}
}
return -1;
}else if(spot==VehicleSize::Compact){
for(int i = 0;i < rows;i++){
for(int j = 0;j < carspots[i].size();j++){
if(carspots[i][j]==0)return i*carspots[i].size()+j;
}
}
return -1;
}else{
for(int i = 0;i < rows;i++){
for(int j = 0;j <= (int)busspots[i].size()-num;j++){
if(busspots[i][j]==0){
bool flag = true;
for(int k = j+1;k < j+num;k++){
if(busspots[i][k]!=0){
flag = false;
}
}
if(flag)return i*busspots[i].size()+j;
}
}
}
return -1;
}
return -1;
}
void parkVehicle(Vehicle &v,VehicleSize spot,int num,int index,int lvl){
if(spot==VehicleSize::Motorcycle){
motorspots[index/motorspots[0].size()][index%motorspots[0].size()] = 1;
}else if(spot==VehicleSize::Compact){
carspots[index/carspots[0].size()][index%carspots[0].size()] = 1;
}else{
for(int i = 0;i <num;i++)busspots[index/busspots[0].size()][index%busspots[0].size()+i] = 1;
}
v.park_vehicle(lvl,index,spot);
}
void unparkVehicle(Vehicle &v){
VehicleSize spot = v.spot_size();
int num = 1;
if(v.get_size()==VehicleSize::Large)num = 5;
int index = v.get_spot();
if(spot==VehicleSize::Motorcycle){
motorspots[index/motorspots[0].size()][index%motorspots[0].size()] = 0;
}else if(spot==VehicleSize::Compact){
carspots[index/carspots[0].size()][index%carspots[0].size()] = 0;
}else{
for(int i = 0;i <num;i++){
busspots[index/busspots[0].size()][index%busspots[0].size()+i] = 0;
}
}
}
};
class ParkingLot {
public:
// @param n number of leves
// @param num_rows each level has num_rows rows of spots
// @param spots_per_row each row has spots_per_row spots
vector<Level*> lvls;
ParkingLot(int n, int num_rows, int spots_per_row) {
// Write your code here
for(int i = 0;i < n;i++){
Level* lvl = new Level(num_rows,spots_per_row);
lvls.push_back(lvl);
}
}
~ParkingLot() {
// Write your code here
for(int i = 0;i < lvls.size();i++){
delete lvls[i];
}
}
// Park the vehicle in a spot (or multiple spots)
// Return false if failed
bool parkVehicle(Vehicle &vehicle) {
// Write your code here
VehicleSize size = vehicle.get_size();
if(size == VehicleSize::Motorcycle){
if(!parkInMotor(vehicle)){
if(!parkInCompact(vehicle)){
return parkInBus(vehicle,1);
}
}
return true;
}else if(size == VehicleSize::Compact){
if(!parkInCompact(vehicle)){
return parkInBus(vehicle,1);
}
return true;
}else{
return parkInBus(vehicle,5);
}
}
bool parkInMotor(Vehicle &v){
int index = -1;
for(int i = 0;i <lvls.size();i++){
index = lvls[i]->canpark(v,VehicleSize::Motorcycle,1);
if(index != -1){
lvls[i]->parkVehicle(v,VehicleSize::Motorcycle,1,index,i);
return true;
}
}
return false;
}
bool parkInCompact(Vehicle &v){
int index = -1;
for(int i = 0;i <lvls.size();i++){
index = lvls[i]->canpark(v,VehicleSize::Compact,1);
if(index != -1){
lvls[i]->parkVehicle(v,VehicleSize::Compact,1,index,i);
return true;
}
}
return false;
}
bool parkInBus(Vehicle &v,int num){
int index = -1;
int i;
for(i = 0;i <lvls.size();i++){
index = lvls[i]->canpark(v,VehicleSize::Large,num);
if(index != -1){
lvls[i]->parkVehicle(v,VehicleSize::Large,num,index,i);
return true;
}
}
return false;
}
// unPark the vehicle
void unParkVehicle(Vehicle &vehicle) {
int i = vehicle.get_level();
if(lvls[i])lvls[i]->unparkVehicle(vehicle);
}
};
int main(){
ParkingLot lot(1,1,14);
Motorcycle Moto_1;
Motorcycle Moto_2;
Motorcycle Moto_3;
Motorcycle Moto_4;
Car car_1;
Car car_2;
Car car_3;
Car car_4;
Car car_5;
Car car_6;
Car car_7;
Bus bus_1;
if(lot.parkVehicle(Moto_1))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(Moto_2))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(Moto_3))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_1))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_2))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_3))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(Moto_4))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_4))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_5))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_6))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(car_7))cout<<"true"<<endl;
else cout<<"false"<<endl;
if(lot.parkVehicle(bus_1))cout<<"true"<<endl;
else cout<<"false"<<endl;
lot.unParkVehicle(car_1);
lot.unParkVehicle(car_3);
lot.unParkVehicle(car_6);
lot.unParkVehicle(Moto_4);
if(lot.parkVehicle(bus_1))cout<<"true"<<endl;
else cout<<"false"<<endl;
lot.unParkVehicle(car_7);
if(lot.parkVehicle(bus_1))cout<<"true"<<endl;
else cout<<"false"<<endl;
return 0;
}