-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashcode_2016_post_contest.cpp
More file actions
277 lines (207 loc) · 7.5 KB
/
hashcode_2016_post_contest.cpp
File metadata and controls
277 lines (207 loc) · 7.5 KB
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
#include "hashcode_2016_post_contest.h"
#define FILE_ID 2
using namespace std;
const string inputFiles[] = { "mother_of_all_warehouses.in", "busy_day.in", "redundancy.in" };
int row, col, droneNB, maxTurn, maxPayload, productNB, warehouseNB, orderNB;
int *productWg, *orderLen, *droneAct;
int **warehouseStock;
pos *warehousePos, *orderPos, *dronePos;
vector<vector<int>> orderItem;
vector<int> currentOrder( 10 );
vector<string> output;
int main() {
parse();
bool finish = false;
for ( int turn = 0; turn < maxTurn && !finish; turn++ ) {
for ( int d = 0; d < droneNB && !finish; d++ ) {
if ( droneAct[d] == 0 ) {
int orderID = getNearestOrder( d );
int warehouseID = getNearestWarehouse( d, orderID );
int droneSpace = maxPayload;
vector<int> items;
int currentItem = getAvailableItemFromOrder( orderID, warehouseID, droneSpace );
while ( currentItem != -1 ) {
items.push_back( currentItem );
removeItemFromOrder( orderID, currentItem );
removeItemFromWarehouse( warehouseID, currentItem );
droneSpace -= productWg[currentItem];
output.push_back( getLoadCmd( d, warehouseID, currentItem, 1 ) );
currentItem = getAvailableItemFromOrder( orderID, warehouseID, droneSpace );
}
for ( unsigned i = 0; i < items.size(); i++ )
output.push_back( getDeliverCmd( d, orderID, items[i], 1 ) );
dronePos[d] = orderPos[orderID];
droneAct[d] += getDistance( dronePos[d], warehousePos[warehouseID] );
droneAct[d] += getDistance( warehousePos[warehouseID], orderPos[orderID] );
finish = isAllOrderEmpty();
}
}
processDroneTurn();
}
freeArray();
printResult( "_" + inputFiles[FILE_ID].substr( 0, inputFiles[FILE_ID].size() - 3 ) + ".out" );
}
void parse() {
ifstream in( inputFiles[FILE_ID].c_str() );
in >> row >> col >> droneNB >> maxTurn >> maxPayload;
droneAct = ( int*) calloc( droneNB, sizeof( int ) );
dronePos = ( pos* ) malloc( droneNB * sizeof( pos ) );
for ( int i = 0; i < droneNB; i++ )
dronePos[i] = pos( 0, 0 );
in >> productNB;
productWg = ( int* ) malloc( productNB * sizeof( int ) );
for ( int i = 0; i < productNB; i++ )
in >> productWg[i];
in >> warehouseNB;
warehousePos = ( pos* ) malloc( warehouseNB * sizeof( pos ) );
warehouseStock = ( int** ) calloc( warehouseNB, sizeof( int* ) );
for ( int i = 0; i < warehouseNB; i++ ) {
int x, y;
in >> x >> y;
warehousePos[i] = pos( x, y );
warehouseStock[i] = ( int* ) calloc( productNB, sizeof( int ) );
for ( int u = 0; u < productNB; u++ )
in >> warehouseStock[i][u];
}
in >> orderNB;
orderPos = ( pos* ) malloc( orderNB * sizeof( pos ) );
orderLen = ( int* ) calloc( orderNB, sizeof( int ) );
orderItem.resize( orderNB );
for ( int i = 0; i < orderNB; i++ ) {
int x, y;
in >> x >> y;
orderPos[i] = pos ( x, y );
int l;
in >> l;
orderLen[i] = l;
vector<int> vec( l );
for ( int u = 0; u < l; u++ )
in >> vec[u];
orderItem[i] = vec;
}
in.close();
}
void freeArray() {
free( productWg );
free( warehousePos );
for ( int i = 0; i < warehouseNB; i++ )
free( warehouseStock[i] );
free( warehouseStock );
free( orderPos );
free( orderLen );
free( droneAct );
free( dronePos );
}
void processDroneTurn() {
for ( int i = 0; i < droneNB; i++ ) {
if ( droneAct[i] > 0 )
droneAct[i]--;
}
}
int getNearestOrder( int droneID ) {
int distance = row * col, orderID = -1;
for ( int o = 0; o < orderNB; o++ ) {
int dt = getDistance( dronePos[droneID], orderPos[o] );
if ( dt < distance && !isOrderEmpty( o ) ) {
distance = dt;
orderID = o;
}
}
return orderID;
}
int getDistance( pos a, pos b ) {
int c = abs( a.x - b.x ), d = abs( a.y - b.y );
return sqrt( c * c + d * d );
}
bool isOrderEmpty( int orderID ) {
for ( int i = 0; i < orderLen[orderID]; i++ ) {
if( orderItem[orderID][i] != -1 )
return false;
}
return true;
}
void removeItemFromOrder( int orderID, int itemID ) {
for ( int p = 0; p < orderLen[orderID]; p++ ) {
if ( orderItem[orderID][p] == itemID ) {
orderItem[orderID][p] = -1;
return;
}
}
cout << "ERROR : " << orderID << " : " << itemID << endl;
}
bool isAllOrderEmpty() {
for ( int o = 0; o < orderNB; o++ ) {
if ( !isOrderEmpty(o) )
return false;
}
return true;
}
int getNearestWarehouse( int droneID, int orderID ) {
int distance = row * col, warehouseID = -1;
for ( int w = 0; w < warehouseNB; w++ ) {
int dt = getDistance( dronePos[droneID], warehousePos[w] );
if ( dt < distance && warehouseContainOrder( orderID, w ) ) {
distance = dt;
warehouseID = w;
}
}
return warehouseID;
}
bool warehouseContainOrder( int orderID, int warehouseID ) {
for ( int i = 0; i < orderLen[orderID]; i++ ) {
if ( orderItem[orderID][i] != -1
&& warehouseStock[warehouseID][orderItem[orderID][i]] > 0 )
return true;
}
return false;
}
string getLoadCmd( int droneID, int warehouseID, int itemID, int nb ) {
return to_string( droneID ) + " L " + to_string( warehouseID ) + " "
+ to_string( itemID ) + " " + to_string( nb );
}
string getUnloadCmd( int droneID, int warehouseID, int itemID, int nb ) {
return to_string( droneID ) + " U " + to_string( warehouseID ) + " "
+ to_string( itemID ) + " " + to_string( nb );
}
string getDeliverCmd( int droneID, int orderID, int itemID, int nb ) {
return to_string( droneID ) + " D " + to_string( orderID ) + " "
+ to_string( itemID ) + " " + to_string(nb);
}
string getWaitCmd( int droneID, int turn ) {
return to_string( droneID ) + " W " + to_string( turn );
}
void printResult( string fileName ) {
ofstream out( fileName.c_str() );
out << output.size() << endl;
ostream_iterator<string> output_iterator( out, "\n" );
copy( output.begin(), output.end(), output_iterator );
out.close();
}
void removeItemFromWarehouse( int warehouseID, int itemID ) {
if ( warehouseStock[warehouseID][itemID] < 1)
cout << "ERROR" << endl;
else
warehouseStock[warehouseID][itemID]--;
}
int getAvailableItemFromOrder( int orderID, int warehouseID, int droneSpace ) {
for ( int o = 0; o < orderLen[orderID]; o++ ) {
if ( orderItem[orderID][o] != -1
&& warehouseStock[warehouseID][orderItem[orderID][o]] > 0
&& productWg[orderItem[orderID][o]] <= droneSpace )
return orderItem[orderID][o];
}
return -1;
}
bool isOrderInProgress( int orderID ) {
if ( isOrderEmpty( orderID ) || orderLen[orderID] == 1 )
return false;
bool itemSend = orderItem[orderID][0] == -1;
for ( int o = 1; o < orderLen[orderID]; o++ ) {
//if ( orderItem[orderID][o] )
}
}
void updateCurrentOrderList() {
currentOrder.erase( remove_if( currentOrder.begin(), currentOrder.end(),[]( int o ) {
return isOrderEmpty( o );
} ), currentOrder.end() );
}