-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal_code.cpp
More file actions
770 lines (739 loc) · 26 KB
/
Copy pathoriginal_code.cpp
File metadata and controls
770 lines (739 loc) · 26 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include <iostream>
#include <vector>
#include <set>
#include <memory>
#include <queue>
#include <unordered_map>
#include <forward_list>
#include <random>
#include <algorithm>
#include <climits>
using namespace std;
/*************************传统参数***************************/
const int n = 200;
const int robot_num = 10;
const int berth_num = 10;
const int N = 202;
int min_transport_time = INT_MAX;
// .空地 *海洋 #障碍 A机器人初始位置 B港口
int money, boat_capacity;
char ch[N][N]; // N=202地图
bool ch_robot[n][n]; // 追踪机器人位置,false表示有机器人
bool final_berth[berth_num]; // 选定的港口, true表示被选中
bool closed_berth[berth_num]; // 已经被关闭的港口, true表示被关闭
// 机器人方向
enum Direction {
RIGHT = 0,
LEFT = 1,
UP = 2,
DOWN = 3
};
Direction directions[] = { RIGHT, LEFT, UP, DOWN };
unordered_map<Direction, pair<int, int>> directions_cord = {
{RIGHT, make_pair(0, 1)},
{LEFT, make_pair(0, -1)},
{UP, make_pair(-1, 0)},
{DOWN, make_pair(1, 0)}
};
// 某点到某个港口的距离和第一步方向
pair<int, Direction> path_to_berth[berth_num][n][n]; // int表示距离,Direction表示第一步方向
int which_berth[n][n]; // 要去的港口
struct Robot
{
public:
void move() {
if (ch_robot[x][y] == false) {
ch_robot[x][y] = true;
}
printf("move %d %d\n", id, action.front());
x += directions_cord[action.front()].first;
y += directions_cord[action.front()].second;
ch_robot[x][y] = false;
action.pop_front();
}
void get() {
printf("get %d\n", id);
}
void pull() {
printf("pull %d\n", id);
}
bool check_no_action() {
return action.empty();
}
void update_action(forward_list<Direction>& new_action) {
action = new_action;
}
int x, y, goods, id, status, dest_berth_id, carry_value;
int dest_goods_loc[2];
forward_list<Direction> action;
}robot[robot_num];
struct Boat
{
void ship(int berth_id) {
printf("ship %d %d\n", id, berth_id);
}
void go() {
printf("go %d\n", id);
}
bool full() {
return goods_num >= boat_capacity;
}
int id, pos;
int status; // 0 表示移动(运输), 1 表示装货状态(装货状态包含等待状态) 2 表示停泊等待状态
int goods_num = 0;
int sum_value = 0; // 已经装的价值
int time = 0; // 已经走的时间
}boat[10];
struct Berth
{
void calculate_edge() {
if (ch[x - 1][y] == '.' && ch[x - 1][y + 3] == '.') {
for (int i = 0; i < 4; ++i) {
near_side[0][i] = x;
near_side[1][i] = y + i;
}
}
else if (ch[x + 4][y] == '.' && ch[x + 4][y + 3] == '.') {
for (int i = 0; i < 4; ++i) {
near_side[0][i] = x + 3;
near_side[1][i] = y + i;
}
}
else if (ch[x][y - 1] == '.' && ch[x + 3][y - 1] == '.') {
for (int i = 0; i < 4; ++i) {
near_side[0][i] = x + i;
near_side[1][i] = y;
}
}
else {
for (int i = 0; i < 4; ++i) {
near_side[0][i] = x + i;
near_side[1][i] = y + 3;
}
}
}
void boat_register(Boat& bt) {
resident_boat_ptr = &bt;
}
void boat_logout() {
resident_boat_ptr = nullptr;
}
void receive_robot(Robot& rob) {
goods_num++;
goods_value_q.push(rob.carry_value);
sum_value += rob.carry_value;
}
void put_boat() { // 表示一个机器人到达
int trans = min(min(goods_num, loading_speed), boat_capacity - resident_boat_ptr->goods_num);
goods_num -= trans;
resident_boat_ptr->goods_num += trans;
for (int i = 0; i < trans; i++) {
resident_boat_ptr->sum_value += goods_value_q.front();
sum_value -= goods_value_q.front();
goods_value_q.pop();
}
}
int x;
int y;
int transport_time;
int loading_speed;
int near_side[2][4]; // 港口靠近边缘4个点
int goods_num = 0; // 港口堆积物品数
int sum_value = 0; // 总价值
queue<int> goods_value_q; // 价值队列
Boat* resident_boat_ptr; // 临时注册在该港口的船
}berth[berth_num];
// 货物
class Goods
{
public:
Goods() {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < 2; ++k) {
gds[i][j][k] = 0;
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
gds_lck[i][j] = -1;
}
}
}
int get_value(int x, int y) const {
return gds[x][y][0];
}
int get_time(int x, int y) const {
return gds[x][y][1];
}
void set(int x, int y, int value, int time) {
gds[x][y][0] = value;
gds[x][y][1] = time;
}
void lock(int x, int y, int id) {
gds_lck[x][y] = id;
}
void unlock(int x, int y) {
gds_lck[x][y] = -1;
}
int lockowner(int x, int y) {
return gds_lck[x][y];
}
void update() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (gds[i][j][0] == 0) {
continue;
}
else if (gds[i][j][1] > 0) {
gds[i][j][1]--;
}
else {
gds[i][j][0] = 0;
}
}
}
}
private:
int gds[n][n][2]; // 第一层值表示价值,第二层值表示时间
int gds_lck[n][n]; // 表示是否有机器人关注一个货物
}goods_map;
// 扩散算法用的块
struct Block {
Block(int x, int y, Direction prev_direction) : x(x), y(y), prev_direction(prev_direction) {}
int x;
int y;
vector<shared_ptr<Block>> sons;
weak_ptr<Block> father;
Direction prev_direction; // 上一步的方向
};
// Astar算法用的块
struct AstarBlock {
AstarBlock(int x, int y, Direction prev_direction, int G, pair<int, int>& dest, shared_ptr<AstarBlock> father) : x(x), y(y), prev_direction(prev_direction), G(G), father(father) {
H = abs(x - dest.first) + abs(y - dest.second);
F = G + H;
}
int x;
int y;
int F; // G + H
int G; // 已走路径
int H; // 启发距离
vector<shared_ptr<AstarBlock>> sons;
weak_ptr<AstarBlock> father;
Direction prev_direction; // 上一步的方向
};
// Astar算法用的小堆,保证最小的F值
class minHeap {
public:
minHeap() : pq(), table(768) {}
void insert(shared_ptr<AstarBlock>& block_ptr) {
if (block_ptr->F >= 768) {
exit(0);
}
table[block_ptr->F].push_front(block_ptr);
pq.push(block_ptr->F);
}
shared_ptr<AstarBlock> pop() {
int m = pq.top();
pq.pop();
shared_ptr<AstarBlock> block_ptr = table[m].front();
table[m].pop_front();
return block_ptr;
}
bool empty() {
return pq.empty();
}
private:
priority_queue<int, vector<int>, greater<int>> pq;
vector<forward_list<shared_ptr<AstarBlock>>> table;
};
/**************************工具函数****************************/
/**
* @brief 寻找最好算法,寻找价值高的货物,通过扩散实现
*
* @param 机器人rob,ret用于接收机器人应走方向
* @return void
*/
void find_goods(Robot& rob, pair<pair<int, int>, forward_list<Direction>>& ret) {
vector<shared_ptr<Block>> root_list; // 防止内存被释放
vector<shared_ptr<Block>> handle_list; // 边界点
vector<shared_ptr<Block>> goods_list; // 货物列表
vector<vector<bool>> close_list(n, vector<bool>(n, true)); // 关闭列表
close_list[rob.x][rob.y] = false;
bool near_berth = false;
int max_value = 0; // 已寻找的货物最大价值
// 当前机器人位置就有货物
if (goods_map.get_value(rob.x, rob.y) != 0 &&
(goods_map.lockowner(rob.x, rob.y) == -1 || goods_map.lockowner(rob.x, rob.y) == rob.id)) { // 确保货物不被其他机器人锁定
ret = make_pair(make_pair(rob.x, rob.y), forward_list<Direction>());
max_value = goods_map.get_value(rob.x, rob.y);
if (abs(rob.x - berth[which_berth[rob.x][rob.y]].near_side[0][rob.id % 4]) +
abs(rob.y - berth[which_berth[rob.x][rob.y]].near_side[1][rob.id % 4]) < 50) { // 货物在港口50内
near_berth = true;
}
}
if (!near_berth) {
for (auto dir : directions) { // 初始化四个方向
int x = rob.x + directions_cord[dir].first;
int y = rob.y + directions_cord[dir].second;
if (x >= 0 && y >= 0 && ch[x][y] != '#' && ch[x][y] != '*' && ch_robot[x][y]) {
shared_ptr<Block> new_block = make_shared<Block>(x, y, dir);
handle_list.push_back(new_block);
root_list.push_back(new_block);
close_list[x][y] = false;
if (goods_map.get_value(new_block->x, new_block->y) != 0 &&
(goods_map.lockowner(new_block->x, new_block->y) == -1 || goods_map.lockowner(new_block->x, new_block->y) == rob.id)) {
goods_list.push_back(new_block);
if (abs(new_block->x - berth[which_berth[new_block->x][new_block->y]].near_side[0][rob.id % 4]) +
abs(new_block->y - berth[which_berth[new_block->x][new_block->y]].near_side[1][rob.id % 4]) < 50) { // 货物在港口50内
near_berth = true;
}
}
}
}
int L = 0;
while (goods_list.size() < 3 && L < 128 && !near_berth) {
L++;
vector<shared_ptr<Block>> tmp_handle_list;
for (auto& block_ptr : handle_list) {
for (auto dir : directions) {
int x = block_ptr->x + directions_cord[dir].first;
int y = block_ptr->y + directions_cord[dir].second;
if (x >= 0 && y >= 0 && ch[x][y] != '#' && ch[x][y] != '*' && ch_robot[x][y] && close_list[x][y]) {
shared_ptr<Block> new_block = make_shared<Block>(x, y, dir);
block_ptr->sons.push_back(new_block);
new_block->father = block_ptr;
tmp_handle_list.push_back(new_block);
close_list[x][y] = false;
if (goods_map.get_value(new_block->x, new_block->y) != 0 && L < goods_map.get_time(new_block->x, new_block->y) + 1 &&
(goods_map.lockowner(new_block->x, new_block->y) == -1 || goods_map.lockowner(new_block->x, new_block->y) == rob.id)) {
goods_list.push_back(new_block);
if (abs(new_block->x - berth[which_berth[new_block->x][new_block->y]].near_side[0][rob.id % 4]) +
abs(new_block->y - berth[which_berth[new_block->x][new_block->y]].near_side[1][rob.id % 4]) < 50) { // 货物在港口50内
near_berth = true;
}
}
}
}
}
handle_list = tmp_handle_list;
}
}
for (auto& goods_ptr : goods_list) {
if (goods_map.get_value(goods_ptr->x, goods_ptr->y) <= max_value) {
continue;
}
max_value = goods_map.get_value(goods_ptr->x, goods_ptr->y);
ret.first = make_pair(goods_ptr->x, goods_ptr->y);
ret.second.clear();
shared_ptr<Block> block_ptr = goods_ptr;
while (block_ptr) {
ret.second.push_front(block_ptr->prev_direction);
block_ptr = block_ptr->father.lock();
}
}
}
/**
* @brief 使用预先计算好的路径寻找泊位,当下一步已经被占用时返回空
*
* @param 机器人rob,ret用于接受下一步动作
* @return void
*/
void find_berth(Robot& rob, forward_list<Direction>& ret) {
if (which_berth[rob.x][rob.y] != -1) { // 确保可以到达港口
rob.dest_berth_id = which_berth[rob.x][rob.y];
ret.push_front(path_to_berth[rob.dest_berth_id][rob.x][rob.y].second);
// 当下一步的机器人被挡住时,返回空的动作列表
int x = rob.x + directions_cord[ret.front()].first;
int y = rob.y + directions_cord[ret.front()].second;
if (ch_robot[x][y] == false) {
ret.clear();
}
}
}
/**
* @brief 机器人的调度,若未携带,寻找价值高的货物;若携带,寻找当前的港口
*
* @param 帧数zhen
* @return void
*/
void robot_dispatch(int zhen) {
for (auto& rob : robot) {
if (rob.status == 0) {
continue;
}
if (rob.goods == 0) {
// 移动前检查
if (rob.check_no_action() && goods_map.get_value(rob.x, rob.y) > 0) { // 机器人站在有货位置上
rob.get();
rob.carry_value = goods_map.get_value(rob.x, rob.y);
goods_map.set(rob.x, rob.y, 0, 0);
goods_map.unlock(rob.x, rob.y);
// 寻路港口
forward_list<Direction> path;
find_berth(rob, path);
rob.update_action(path);
}
else {
if (true) {
// 寻路货物
pair<pair<int, int>, forward_list<Direction>> path;
find_goods(rob, path);
// 更新锁定货物
if (path.first.first != rob.dest_goods_loc[0] || path.first.second != rob.dest_goods_loc[1]) {
goods_map.unlock(rob.dest_goods_loc[0], rob.dest_goods_loc[1]);
rob.dest_goods_loc[0] = path.first.first;
rob.dest_goods_loc[1] = path.first.second;
goods_map.lock(rob.dest_goods_loc[0], rob.dest_goods_loc[1], rob.id);
}
rob.update_action(path.second);
}
}
// 移动
if (!rob.check_no_action()) {
rob.move();
}
}
else {
// 移动前检查
if (ch[rob.x][rob.y] == 'B') { // 在港口上方,取货后寻路
rob.pull();
berth[rob.dest_berth_id].receive_robot(rob);
// 寻路货物
pair<pair<int, int>, forward_list<Direction>> path;
find_goods(rob, path);
rob.dest_goods_loc[0] = path.first.first;
rob.dest_goods_loc[1] = path.first.second;
goods_map.lock(rob.dest_goods_loc[0], rob.dest_goods_loc[1], rob.id);
rob.update_action(path.second);
}
else {
if (true) {
// 寻路港口
forward_list<Direction> path;
find_berth(rob, path);
rob.update_action(path);
}
}
// 移动
if (!rob.check_no_action()) {
rob.move();
}
}
}
}
/**
* @brief 船只的调度,若未满载,停靠有货物的港口
*
* @param 帧数zhen
* @return void
*/
void boat_dispatch(int zhen) {
for (int i = 0; i < 10; i++) { // 这里要先进行,让港口空出来
if (boat[i].status == 1 && boat[i].pos > -1) {
int res_berth_id = boat[i].pos;
// 最后一帧上船, 而港口不足5个时, 关闭港口
if (zhen > 15000 && !final_berth[res_berth_id]) {
closed_berth[res_berth_id] = true;
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
if (which_berth[j][k] == res_berth_id) {
int max_L = INT_MAX;
which_berth[j][k] = -1;
for (int s = 0; s < berth_num; s++) {
if (closed_berth[s]) continue;
if (path_to_berth[s][j][k].first <= max_L) {
which_berth[j][k] = s;
max_L = path_to_berth[s][j][k].first;
}
}
}
}
}
}
if (zhen > 14998 - berth[res_berth_id].transport_time || // 到时间了
boat[i].full()) // 满了
{
boat[i].go();
berth[res_berth_id].boat_logout();
cerr << "Debug info: boat " << i << " go zhen " << zhen << " goods " << boat[i].goods_num << " full " << boat[i].full() << endl;
boat[i].goods_num = 0;
boat[i].time = 0;
boat[i].sum_value = 0;
}
else if (berth[res_berth_id].goods_num == 0) { // 当前港口没货物,移动
int berth_i = -1;
// max_score表示即时返回的价值, 若剩余时间不够返回一次,返回对应分数
float max_score = zhen < 14990 - berth[res_berth_id].transport_time - 2 * min_transport_time ?
(float)boat[i].sum_value / (boat[i].time + berth[res_berth_id].transport_time) : -1;
for (int j = 0; j < berth_num; j++) {
if (berth[j].resident_boat_ptr || zhen > 14490 - berth[j].transport_time || berth[j].goods_num == 0) {
continue;
}
float score = ((float)berth[j].sum_value * min(berth[j].goods_num + 4, boat_capacity - boat[i].goods_num) /
berth[j].goods_num + boat[i].sum_value) / (boat[i].time + 500 + berth[j].transport_time);
if (score > max_score) {
berth_i = j;
max_score = score;
}
}
if (berth_i != -1) {
boat[i].ship(berth_i);
berth[res_berth_id].boat_logout();
berth[berth_i].boat_register(boat[i]);
boat[i].time += 500;
}
else { // 没找到合适的港口
if (true) {
cerr << "Debug info: boat " << i << " go zhen " << zhen << " goods " << boat[i].goods_num << " full " << boat[i].full() << endl;
boat[i].go();
berth[res_berth_id].boat_logout();
boat[i].goods_num = 0;
boat[i].time = 0;
boat[i].sum_value = 0;
}
}
}
}
}
for (int i = 0; i < berth_num; i++) { // 装货需要在注册之前
if (berth[i].resident_boat_ptr != nullptr && berth[i].resident_boat_ptr->status == 1) {
berth[i].put_boat();
}
}
for (int i = 0; i < 10; i++) { // 注册到港口
if (boat[i].status == 1 && boat[i].pos == -1) {
int berth_i = -1;
float max_score = -1;
for (int j = 0; j < berth_num; j++) {
if (berth[j].resident_boat_ptr) {
continue;
}
float score = (float)berth[j].sum_value / (2 * berth[j].transport_time);
if (score > max_score) {
max_score = score;
berth_i = j;
}
}
boat[i].ship(berth_i);
berth[berth_i].boat_register(boat[i]);
boat[i].time += berth[berth_i].transport_time;
}
}
}
/**
* @brief 初始化时对全地图点到某个港口的最短路, 并选择5个最终港口
*
* @param void
* @return void
*/
void init_path() {
for (int i = 0; i < berth_num; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
path_to_berth[i][j][k].first = INT_MAX;
}
}
}
for (int i = 0; i < berth_num; i++) {
vector<pair<int, int>> handle_list; // 扩散中的坐标集合
vector<vector<bool>> close_list(n, vector<bool>(n, true)); // 表示已处理集合
for (int j = 0; j < 4; j++) {
handle_list.push_back(make_pair(berth[i].near_side[0][j], berth[i].near_side[1][j]));
close_list[berth[i].near_side[0][j]][berth[i].near_side[1][j]] = false;
}
int L = 0;
while (!handle_list.empty()) {
L++;
vector<pair<int, int>> tmp_handle_list;
for (auto& point : handle_list) {
for (auto dir : directions) {
int x = point.first - directions_cord[dir].first; // 注意,要反向
int y = point.second - directions_cord[dir].second;
if (x >= 0 && y >= 0 && ch[x][y] != '#' && ch[x][y] != '*' && ch[x][y] != 'B' && close_list[x][y]) {
path_to_berth[i][x][y].first = L;
path_to_berth[i][x][y].second = dir;
tmp_handle_list.push_back(make_pair(x, y));
close_list[x][y] = false;
}
}
}
handle_list = tmp_handle_list;
}
}
// 更新每个点对应的港口
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
int m = 0;
which_berth[j][k] = -1;
for (int i = 0; i < berth_num; i++) {
if (path_to_berth[i][j][k].first <= path_to_berth[m][j][k].first) {
which_berth[j][k] = i;
m = i;
}
}
}
}
// 判断哪些机器人可达berth互相可达
set<vector<int>> berth_group;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (ch_robot[i][j] = true) {
vector<int> group;
for (int k = 0; k < berth_num; k++) {
if (path_to_berth[k][i][j].first != INT_MAX) {
group.push_back(k);
}
}
if (!group.empty()) {
berth_group.insert(group);
}
}
}
}
// 选择5个最终港口, 目前策略基于连通分量大小
int res_num = 5;
random_device rd;
mt19937 gen(rd());
for (int i = 0; i < berth_num; i++) {
final_berth[i] = false;
closed_berth[i] = false;
}
while (res_num > 0 && !berth_group.empty()) {
size_t max_size = 0;
auto max_size_it = berth_group.begin();
for (auto it = berth_group.begin(); it != berth_group.end(); it++) {
if ((*it).size() > max_size) {
max_size = (*it).size();
max_size_it = it;
}
}
vector<int> vec = *max_size_it;
berth_group.erase(max_size_it);
shuffle(vec.begin(), vec.end(), gen);
int curr_num = 0;
if (max_size > 8) {
curr_num = min(res_num, 5);
}
else if (max_size > 6) {
curr_num = min(res_num, 4);
}
else if (max_size > 5) {
curr_num = min(res_num, 3);
}
else if (max_size > 3) {
curr_num = min(res_num, 2);
}
else {
curr_num = min(res_num, 1);
}
for (int i = 0; i < curr_num; i++) {
final_berth[vec[i]] = true;
}
res_num -= curr_num;
}
}
void Init()
{
for (int i = 0; i < n; i++) {
scanf("%s", ch[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (ch[i][j] == 'A') {
ch[i][j] = '.';
}
ch_robot[i][j] = true;
}
}
for (int i = 0; i < N; i++) { // 右边界和下边界设置为墙
ch[n][i] = ch[i][n] = '#';
}
for(int i = 0; i < berth_num; i ++)
{
int id;
scanf("%d", &id);
scanf("%d%d%d%d", &berth[id].x, &berth[id].y, &berth[id].transport_time, &berth[id].loading_speed);
if (berth[id].transport_time < min_transport_time) {
min_transport_time = berth[id].transport_time;
}
berth[id].calculate_edge();
}
scanf("%d", &boat_capacity); // 71
char okk[100];
scanf("%s", okk);
init_path();
printf("OK\n");
fflush(stdout);
}
void First_Input() // 第一帧的读取,需要把对应id的机器人对应位置
{
int id;
scanf("%d%d", &id, &money);
int num;
scanf("%d", &num);
for (int i = 1; i <= num; i++)
{
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
goods_map.set(x, y, val, 1000);
}
for (int i = 0; i < robot_num; i++)
{
scanf("%d%d%d%d", &robot[i].goods, &robot[i].x, &robot[i].y, &robot[i].status);
robot[i].id = i;
}
for (int i = 0; i < 5; i++) {
scanf("%d%d\n", &boat[i].status, &boat[i].pos);
boat[i].id = i;
}
char okk[100];
scanf("%s", okk);
}
void Input(int zhen)
{
int id;
scanf("%d%d", &id, &money);
goods_map.update();
int num;
scanf("%d", &num);
for(int i = 1; i <= num; i ++)
{
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
goods_map.set(x, y, val, 1000);
}
for(int i = 0; i < robot_num; i ++)
{
// 这里更新的机器人信息是不断更新的,所以每一帧需要更新机器人位置信息
ch_robot[robot[i].x][robot[i].y] = true;
scanf("%d%d%d%d", &robot[i].goods, &robot[i].x, &robot[i].y, &robot[i].status);
ch_robot[robot[i].x][robot[i].y] = false;
}
for (int i = 0; i < 5; i++) {
scanf("%d%d\n", &boat[i].status, &boat[i].pos);
}
char okk[100];
scanf("%s", okk);
}
void Output(int zhen) {
robot_dispatch(zhen);
boat_dispatch(zhen);
puts("OK");
fflush(stdout);
}
/**************************主函数****************************/
int main()
{
Init();
First_Input();
puts("OK");
fflush(stdout);
for(int zhen = 2; zhen <= 15000; zhen ++)
{
Input(zhen);
Output(zhen);
}
return 0;
}