-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·218 lines (192 loc) · 5.11 KB
/
main.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
#include "Berth.h"
#include "Boat.h"
#include "Robot.h"
#include "Cargo.h"
#include "tool.h"
using namespace std;
int money, FrameId;
// 输入的字符地图
char ChMap[WIDTH][WIDTH];
// 网格图,0表示可通行,1表示障碍
int Blocks[WIDTH][WIDTH];
Robot Robots[ROBOT_NUM];
Boat Boats[BOAT_NUM];
vector<Berth *> BerthList;
set<Cargo *> CargoSet;
queue<Cargo *> CargoQueue;
map<int, int> frame2K;
int Boat::capacity = 0;
void Init()
{
// 地图信息
for (int i = 0; i < WIDTH; i++)
{
for (int j = 0; j < WIDTH; j++)
{
cin >> ChMap[i][j];
// 初始化障碍图
if (ChMap[i][j] == '.' || ChMap[i][j] == 'B' || ChMap[i][j] == 'A')
{
Blocks[i][j] = 0; // 无障碍
}
else
{
Blocks[i][j] = 1; // 有障碍
}
}
}
// 机器人初始化
for (int i = 0; i < ROBOT_NUM; i++)
Robots[i].id = i;
// 泊位信息
BerthList.resize(BERTH_NUM);
for (int i = 0; i < BERTH_NUM; i++)
{
int id, x, y, transport_time, loading_speed;
cin >> id >> x >> y >> transport_time >> loading_speed;
BerthList[id] = new Berth(id, x, y, transport_time, loading_speed);
}
// 船只信息
for (int i = 0; i < BOAT_NUM; i++)
Boats[i].id = i;
cin >> Boat::capacity;
// OK
string info_end;
cin >> info_end;
cout << "OK" << endl;
;
fflush(stdout);
}
void Input()
{
cin >> FrameId >> money;
int K;
cin >> K;
if (K != 0)
frame2K[FrameId] = K;
// 处理失效的货物
auto it = frame2K.begin();
while (it != frame2K.end())
{
int frame = it->first;
int K = it->second;
if (frame <= FrameId - CARGO_LEFT_TIME)
{
for (int i = 0; i < K; i++)
{
Cargo *cargo = CargoQueue.front();
CargoQueue.pop();
CargoSet.erase(cargo);
if (Robots[cargo->selected].is_carring_cargo)
{
// 被选择这个货的机器人取到了就不删除
continue;
}
else
{
delete cargo;
}
}
it = frame2K.erase(it);
}
else
{
++it;
}
}
// 新生成的货物信息
vector<Cargo *> newCargoList;
for (int i = 0; i < K; i++)
{
int x, y, val;
cin >> x >> y >> val;
auto *c = new Cargo(x, y, val);
CargoSet.insert(c);
CargoQueue.push(c);
newCargoList.push_back(c);
}
// 输入机器人信息
for (int i = 0; i < ROBOT_NUM; i++)
{
cin >> Robots[i].is_carring_cargo >> Robots[i].x >> Robots[i].y >> Robots[i].is_running;
if (Robots[i].path.size() != 0 && Robots[i].path_index < Robots[i].path.size() - 1)
{
if (Robots[i].x == Robots[i].path[Robots[i].path_index + 1].x && Robots[i].y == Robots[i].path[Robots[i].path_index + 1].y)
Robots[i].path_index++;
}
}
// 泊位信息初始化
for (int i = 0; i < BERTH_NUM; i++)
BerthList[i]->selected_ship_num = 0;
// 船信息
for (int i = 0; i < BOAT_NUM; i++)
{
int state, target;
cin >> state >> target;
Boats[i].state = state;
Boats[i].target = target;
if (target != -1)
BerthList[target]->selected_ship_num++;
}
string OK;
cin >> OK;
}
void InitRobotState()
{
// 初始化,找出哪些机器人是被困的
for (int i = 0; i < ROBOT_NUM; i++)
{
Point berth_point(BerthList[i]->x, BerthList[i]->y);
bool try_find = Robots[i].is_available(Blocks, berth_point);
if (try_find == false)
{
BerthList[i]->select_failed_robots.insert(&Robots[i]);
bool trapped = true;
for (int j = 0; j < BERTH_NUM; j++)
{
if (j == i)
continue;
Point berth(BerthList[j]->x, BerthList[j]->y);
if (Robots[i].is_available(Blocks, berth))
{
trapped = false;
break;
}
else
BerthList[j]->select_failed_robots.insert(&Robots[i]);
}
if (trapped)
Robots[i].dead = true;
}
}
}
// 防止astar调用过多
int Search::count_a = 0;
int Search::count_b = 0;
int Search::count = 0;
int main()
{
Init();
for (int frame = 1; frame <= 15000; frame++)
{
Input();
// 寻路次数初始化
Search::count = 0;
// 机器人初始化
if (frame == 1)
InitRobotState();
// 机器人行动
for (int i = 0; i < ROBOT_NUM; i++)
{
Robots[i].act(Blocks, CargoSet, BerthList, Robots);
}
// 船只行动
for (int i = 0; i < BOAT_NUM; i++)
{
Boats[i].act(BerthList);
}
cout << "OK" << endl;
fflush(stdout);
}
return 0;
}