-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijkstras_compile_test.c
420 lines (365 loc) · 9.64 KB
/
dijkstras_compile_test.c
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
////////////////////////////////////////
//
// File : ai.c
// CoSpace Robot
// Version 1.0.0
// Jan 1 2016
// Copyright (C) 2016 CoSpace Robot. All Rights Reserved
//
//////////////////////////////////////
//
// ONLY C Code can be compiled.
//
/////////////////////////////////////
#define CsBot_AI_H//DO NOT delete this line
#ifndef CSBOT_REAL
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
typedef struct {
int vertex;
int weight;
} edge_t;
typedef struct {
edge_t **edges;
int edges_len;
int edges_size;
int dist;
int prev;
int visited;
} vertex_t;
typedef struct {
vertex_t **vertices;
int vertices_len;
int vertices_size;
} graph_t;
typedef struct {
int *data;
int *prio;
int *index;
int len;
int size;
} heap_t;
#define DLL_EXPORT extern __declspec(dllexport)
#define false 0
#define true 1
#endif//The robot ID : It must be two char, such as '00','kl' or 'Cr'.
char AI_MyID[2] = {'0','2'};
int count=0;
int Duration = 0;
int SuperDuration = 0;
int bGameEnd = false;
int CurAction = -1;
int CurGame = 0;
int SuperObj_Num = 0;
int SuperObj_X = 0;
int SuperObj_Y = 0;
int Teleport = 0;
int LoadedObjects = 0;
int US_Front = 0;
int US_Left = 0;
int US_Right = 0;
int CSLeft_R = 0;
int CSLeft_G = 0;
int CSLeft_B = 0;
int CSRight_R = 0;
int CSRight_G = 0;
int CSRight_B = 0;
int PositionX = 0;
int PositionY = 0;
int TM_State = 0;
int Compass = 0;
int Time = 0;
double WheelLeft = 0;
double WheelRight = 0;
int LED_1 = 0;
int MyState = 0;
int AI_SensorNum = 13;
#define CsBot_AI_C//DO NOT delete this line
void add_vertex (graph_t *g, int i);
void add_edge (graph_t *g, int a, int b, int w);
heap_t *create_heap (int n);
void push_heap (heap_t *h, int v, int p);
int minimum (heap_t *h, int i, int j, int k);
int pop_heap (heap_t *h);
void dijkstra (graph_t *g, int a, int b);
void print_path (graph_t *g, int i);
void generate_undirected_edge(graph_t *g, int source, int destination, int weight);
DLL_EXPORT void SetGameID(int GameID)
{
CurGame = GameID;
bGameEnd = 0;
}
DLL_EXPORT int GetGameID()
{
return CurGame;
}
//Only Used by CsBot Dance Platform
DLL_EXPORT int IsGameEnd()
{
return bGameEnd;
}
#ifndef CSBOT_REAL
DLL_EXPORT char* GetDebugInfo()
{
char info[1024];
sprintf(info, "Duration=%d;SuperDuration=%d;bGameEnd=%d;CurAction=%d;CurGame=%d;SuperObj_Num=%d;SuperObj_X=%d;SuperObj_Y=%d;Teleport=%d;LoadedObjects=%d;US_Front=%d;US_Left=%d;US_Right=%d;CSLeft_R=%d;CSLeft_G=%d;CSLeft_B=%d;CSRight_R=%d;CSRight_G=%d;CSRight_B=%d;PositionX=%d;PositionY=%d;TM_State=%d;Compass=%d;Time=%d;WheelLeft=%d;WheelRight=%d;LED_1=%d;MyState=%d;",Duration,SuperDuration,bGameEnd,CurAction,CurGame,SuperObj_Num,SuperObj_X,SuperObj_Y,Teleport,LoadedObjects,US_Front,US_Left,US_Right,CSLeft_R,CSLeft_G,CSLeft_B,CSRight_R,CSRight_G,CSRight_B,PositionX,PositionY,TM_State,Compass,Time,WheelLeft,WheelRight,LED_1,MyState);
return info;
}
DLL_EXPORT char* GetTeamName()
{
return "CoSpace Team";
}
DLL_EXPORT int GetCurAction()
{
return CurAction;
}
//Only Used by CsBot Rescue Platform
DLL_EXPORT int GetTeleport()
{
return Teleport;
}
//Only Used by CsBot Rescue Platform
DLL_EXPORT void SetSuperObj(int X, int Y, int num)
{
SuperObj_X = X;
SuperObj_Y = Y;
SuperObj_Num = num;
}
//Only Used by CsBot Rescue Platform
DLL_EXPORT void GetSuperObj(int *X, int *Y, int *num)
{
*X = SuperObj_X;
*Y = SuperObj_Y;
*num = SuperObj_Num;
}
#endif ////CSBOT_REAL
DLL_EXPORT void SetDataAI(volatile int* packet, volatile int *AI_IN)
{
int sum = 0;
US_Front = AI_IN[0]; packet[0] = US_Front; sum += US_Front;
US_Left = AI_IN[1]; packet[1] = US_Left; sum += US_Left;
US_Right = AI_IN[2]; packet[2] = US_Right; sum += US_Right;
CSLeft_R = AI_IN[3]; packet[3] = CSLeft_R; sum += CSLeft_R;
CSLeft_G = AI_IN[4]; packet[4] = CSLeft_G; sum += CSLeft_G;
CSLeft_B = AI_IN[5]; packet[5] = CSLeft_B; sum += CSLeft_B;
CSRight_R = AI_IN[6]; packet[6] = CSRight_R; sum += CSRight_R;
CSRight_G = AI_IN[7]; packet[7] = CSRight_G; sum += CSRight_G;
CSRight_B = AI_IN[8]; packet[8] = CSRight_B; sum += CSRight_B;
PositionX = AI_IN[9]; packet[9] = PositionX; sum += PositionX;
PositionY = AI_IN[10]; packet[10] = PositionY; sum += PositionY;
TM_State = AI_IN[11]; packet[11] = TM_State; sum += TM_State;
Compass = AI_IN[12]; packet[12] = Compass; sum += Compass;
Time = AI_IN[13]; packet[13] = Time; sum += Time;
packet[14] = sum;
}
DLL_EXPORT void GetCommand(int *AI_OUT)
{
AI_OUT[0] = WheelLeft;
AI_OUT[1] = WheelRight;
AI_OUT[2] = LED_1;
AI_OUT[3] = MyState;
}
void Game0()
{
static int count=0;
if(count==0){
graph_t *g = calloc(1, sizeof (graph_t));
generate_undirected_edge(g,'a','c',3);
add_edge(g, 'a', 'b', 7);
add_edge(g, 'b', 'a', 7);
add_edge(g, 'c', 'b', 1);
add_edge(g, 'b', 'c', 1);
//dijkstra function computes shortest path from second arg to third arg.
//dijkstra(g,source_v,destination_v)
dijkstra(g, 'a', 'b');
//print_path(g,destination_v)
print_path(g, 'b');
dijkstra(g, 'b', 'a');
//print_path(g,destination_v)
print_path(g, 'a');
count=1;
}
}
void Game1()
{
if(SuperDuration>0)
{
SuperDuration--;
}
else if(Duration>0)
{
Duration--;
}
else if(true)
{
Duration = 0;
CurAction =1;
}
switch(CurAction)
{
case 1:
WheelLeft=1.55;
WheelRight=1.55;
LED_1=0;
MyState=0;
break;
default:
break;
}
}
DLL_EXPORT void OnTimer()
{
switch (CurGame)
{
case 9:
break;
case 10:
WheelLeft=0;
WheelRight=0;
LED_1=0;
MyState=0;
break;
case 0:
printf("loop count: %d\n", count);
count++;
Game0();
break;
case 1:
Game1();
break;
default:
break;
}
}
void generate_undirected_edge(graph_t *g, int source, int destination, int weight){
add_edge(g, source, destination, weight);
add_edge(g, destination, source, weight);
}
void add_vertex (graph_t *g, int i) {
if (g->vertices_size < i + 1) {
int size = g->vertices_size * 2 > i ? g->vertices_size * 2 : i + 4;
g->vertices = realloc(g->vertices, size * sizeof (vertex_t *));
for (int j = g->vertices_size; j < size; j++)
g->vertices[j] = NULL;
g->vertices_size = size;
}
if (!g->vertices[i]) {
g->vertices[i] = calloc(1, sizeof (vertex_t));
g->vertices_len++;
}
}
void add_edge (graph_t *g, int a, int b, int w) {
a = a - 'a';
b = b - 'a';
add_vertex(g, a);
add_vertex(g, b);
vertex_t *v = g->vertices[a];
if (v->edges_len >= v->edges_size) {
v->edges_size = v->edges_size ? v->edges_size * 2 : 4;
v->edges = realloc(v->edges, v->edges_size * sizeof (edge_t *));
}
edge_t *e = calloc(1, sizeof (edge_t));
e->vertex = b;
e->weight = w;
v->edges[v->edges_len++] = e;
}
heap_t *create_heap (int n) {
heap_t *h = calloc(1, sizeof (heap_t));
h->data = calloc(n + 1, sizeof (int));
h->prio = calloc(n + 1, sizeof (int));
h->index = calloc(n, sizeof (int));
return h;
}
void push_heap (heap_t *h, int v, int p) {
int i = h->index[v] == 0 ? ++h->len : h->index[v];
int j = i / 2;
while (i > 1) {
if (h->prio[j] < p)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
j = j / 2;
}
h->data[i] = v;
h->prio[i] = p;
h->index[v] = i;
}
int minimum (heap_t *h, int i, int j, int k) {
int m = i;
if (j <= h->len && h->prio[j] < h->prio[m])
m = j;
if (k <= h->len && h->prio[k] < h->prio[m])
m = k;
return m;
}
int pop_heap (heap_t *h) {
int v = h->data[1];
int i = 1;
while (1) {
int j = minimum(h, h->len, 2 * i, 2 * i + 1);
if (j == h->len)
break;
h->data[i] = h->data[j];
h->prio[i] = h->prio[j];
h->index[h->data[i]] = i;
i = j;
}
h->data[i] = h->data[h->len];
h->prio[i] = h->prio[h->len];
h->index[h->data[i]] = i;
h->len--;
return v;
}
void dijkstra (graph_t *g, int a, int b) {
int i, j;
a = a - 'a';
b = b - 'a';
for (i = 0; i < g->vertices_len; i++) {
vertex_t *v = g->vertices[i];
v->dist = INT_MAX;
v->prev = 0;
v->visited = 0;
}
vertex_t *v = g->vertices[a];
v->dist = 0;
heap_t *h = create_heap(g->vertices_len);
push_heap(h, a, v->dist);
while (h->len) {
i = pop_heap(h);
if (i == b)
break;
v = g->vertices[i];
v->visited = 1;
for (j = 0; j < v->edges_len; j++) {
edge_t *e = v->edges[j];
vertex_t *u = g->vertices[e->vertex];
if (!u->visited && v->dist + e->weight <= u->dist) {
u->prev = i;
u->dist = v->dist + e->weight;
push_heap(h, e->vertex, u->dist);
}
}
}
}
void print_path (graph_t *g, int i) {
int n, j;
vertex_t *v, *u;
i = i - 'a';
v = g->vertices[i];
if (v->dist == INT_MAX) {
printf("no path\n");
return;
}
for (n = 1, u = v; u->dist; u = g->vertices[u->prev], n++)
;
char *path = malloc(n);
path[n - 1] = 'a' + i;
for (j = 0, u = v; u->dist; u = g->vertices[u->prev], j++)
path[n - j - 2] = 'a' + u->prev;
printf("%d %.*s\n", v->dist, n, path);
}