-
Notifications
You must be signed in to change notification settings - Fork 0
/
Strategy.hpp
192 lines (158 loc) · 3.7 KB
/
Strategy.hpp
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
#ifndef STRATEGY_HPP
#define STRATEGY_HPP
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <algorithm>
#include <bitset>
constexpr int LEVELS_COUNT = 9;
constexpr int OPENING_TICS = 100;
constexpr int CLOSING_TICS = 100;
constexpr int MIN_FILLING_TICS = 40;
constexpr int MAX_PASSENGERS = 20;
enum class PState
{
WAITING_FOR_ELEVATOR,
MOVING_TO_ELEVATOR,
RETURNING,
MOVING_TO_FLOOR,
USING_ELEVATOR,
EXITING,
OUT,
COUNT
};
enum class Side {
LEFT, RIGHT
};
inline Side inverseSide(Side side)
{
if (side == Side::LEFT)
return Side::RIGHT;
return Side::LEFT;
}
enum class EState
{
WAITING,
MOVING,
OPENING,
FILLING,
CLOSING
};
const char * getPStateName(PState s);
const char * getEStateName(EState s);
struct MyPassenger
{
int id; // идентификатор
double x, y; // координаты
double spawn_x;
int from_floor, dest_floor; // этаж появления и цели
int time_to_away;
Side side;
int elevator; // идентификатор назначенного лифта
PState state;
int ticks = 0;
int placesRemained;
int places[5];
std::bitset<LEVELS_COUNT> visitedLevels;
float mass;
bool dest_floor_confirmed;
int getFloor() const
{
return y;
}
int getValue(Side mySide) const
{
int res = std::abs(dest_floor - from_floor) * 10;
if (side != mySide)
res *= 2;
return res;
}
double getValue2(Side mySide) const;
std::set<int> set_elevator;
};
struct Simulator;
struct MyElevator
{
int id;
int ind;
double x, y;
double speed;
int time_on_the_floor_with_opened_doors;
int closing_or_opening_ticks;
int next_floor;
double time_to_floor;
Side side;
EState state;
std::set<int> passengers;
int go_to_floor = -1;
int getFloor() const
{
return y;
}
void setState(EState state, Simulator &sim);
double calcTimeToFloor(double f, Simulator &sim) const;
};
struct Value {
double points = 0.0;
int ticks = 0;
int uniqueLevels = 0;
};
struct Random
{
uint32_t m_w = 12345; /* must not be zero, nor 0x464fffff */
uint32_t m_z = 456345; /* must not be zero, nor 0x9068ffff */
uint32_t get_random()
{
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w; /* 32-bit result */
}
int generateRandFloors(int floors_out[], int size, const std::bitset<LEVELS_COUNT> &visitedLevels)
{
int levels[LEVELS_COUNT - 1];
int j = 0;
for (int i = 0; i < LEVELS_COUNT; ++i)
{
if (!visitedLevels.test(i))
levels[j++] = i;
}
if (j > 1)
std::random_shuffle(levels, levels + j, [this](int n){return get_random() % n;});
if (j > size)
j = size;
std::copy(levels, levels + j, floors_out);
return j;
}
};
struct Simulator
{
Simulator();
void synchronizeWith(const Simulator &sim);
void copyCommandsFrom(const Simulator &sim, Side side);
typedef std::unordered_map<int, MyPassenger> Passengers;
Passengers passengers;
std::vector<MyElevator> elevators;
std::multimap<int, MyPassenger> outPassengers;
int scores[2] = {};
int scoresByElevators[8] = {};
int passengersTotal[2] = {};
int tick = -1;
int cur_pass_seq = 0;
void step();
void applyElevatorGoToFloorCommands();
void appySetElevatorToPassengerCommands();
void simulatePassengers();
void simulateElevators();
void addPassengers();
void randomizePassenger(MyPassenger &passenger);
void randomizePassengers();
Random random;
Value getCargoValue(MyElevator &elevator);
Value getCargoValueBug(MyElevator &elevator);
Value totalCargoValue(Side side);
Value getCargoValue2(MyElevator &elevator);
Value totalCargoValue2(Side side);
int ramdomFloor();
};
#endif