-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigator.cpp
executable file
·231 lines (190 loc) · 5.46 KB
/
navigator.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
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <stdlib.h>
#define MAX_CROSSROADS 10
#define CROSSROADS_EPS 1.
#define D_INFTY (1e99)
struct Point
{
double x;
double y;
};
class Navigator
{
public:
void initialize()
{
nCrossroads = 0;
nExits = 0;
prevDirection = -1;
}
int navigate(Point& position, int direction, double distance)
{
sprintf(buffer, "\nnavigate() called with position = [%lf, %lf], direction = %d, distance = %lf\n",
position.x, position.y, direction, distance);
Log(buffer);
int maxi = -1;
for(int i = 0; i < nCrossroads; i++)
{
if(sqdist(position, crossroads[i]) < CROSSROADS_EPS)
{
if(maxi == -1)
{
maxi = i;
}
else
{
sprintf(buffer, "WARNING!!! Crossroads #%d is also a candidate!\n", i);
Log(buffer);
}
}
}
if(maxi == -1)
{
sprintf(buffer, "considered a new crossroads #%d\n", nCrossroads);
Log(buffer);
maxi = nCrossroads++;
crossroads[maxi] = position;
nExits += 4;
for(int i = 0; i < 4; i++)
{
fol[maxi][i] = -1;
}
}
else
{
sprintf(buffer, "recognized as crossroads #%d\n", maxi);
Log(buffer);
position = crossroads[maxi];
}
if((prevDistance != -1) && (fol[prevCrossroads][prevDirection] == -1))
{
fol[prevCrossroads][prevDirection] = maxi;
dist[prevCrossroads][prevDirection] = distance - prevDistance;
fol[maxi][direction ^ 2] = prevCrossroads;
dist[maxi][direction ^ 2] = distance - prevDistance;
nExits -= 2;
sprintf(buffer, "crossroads #%d exit %d connects to crossroads #%d exit %d, distance = %lf\n",
prevCrossroads, prevDirection, maxi, direction ^ 2, distance - prevDistance);
Log(buffer);
}
if(!(nExits))
{
sprintf(buffer, "navigate() returns TERMINATE\n\n");
Log(buffer);
return -2;
}
int newDirection = getNewDirection(maxi, direction ^ 2);
prevCrossroads = maxi;
prevDirection = newDirection;
prevDistance = distance;
int turn = ((newDirection + (direction * 3) + 1) & 3) - 1;
sprintf(buffer, "navigate() returns %d\n\n", turn);
Log(buffer);
return turn;
}
private:
char buffer[200];
int nCrossroads;
Point crossroads[MAX_CROSSROADS];
int fol[MAX_CROSSROADS][4];
double dist[MAX_CROSSROADS][4];
int prevCrossroads;
int prevDirection;
double prevDistance;
int nExits;
double searchDist[MAX_CROSSROADS];
int visited[MAX_CROSSROADS];
double sqdist(const Point& p, const Point& q)
{
return ((p.x - q.x) * (p.x - q.x)) + ((p.y - q.y) * (p.y - q.y));
}
void Log(const char* Text)
{
printf("%s", Text);
}
int getNewDirection(int c, int fd)
{
int a[3], n = 0;
for(int i = 0; i < 4; i++)
{
if((i != fd) && (fol[c][i] == -1))
{
a[n++] = i;
}
}
if(n)
{
int rsl = a[rand() % n];
sprintf(buffer, "selecting random undiscovedred direction %d\n", rsl);
Log(buffer);
return rsl;
}
double min = D_INFTY;
int rsl = -1;
for(int i = 0; i < 4; i++)
{
if(i == fd)
{
continue;
}
for(int j = 0; j < nCrossroads; j++)
{
visited[j] = 0;
searchDist[j] = D_INFTY;
}
int t = fol[c][i];
searchDist[t] = dist[c][i];
int cur;
while(1)
{
double mincur = D_INFTY;
cur = -1;
for(int j = 0; j < nCrossroads; j++)
{
if((!(visited[j])) && (searchDist[j] < mincur))
{
mincur = searchDist[j];
cur = j;
}
}
visited[cur] = 1;
for(int j = 0; j < 4; j++)
{
if(fol[cur][j] == -1)
{
goto FOUND;
}
}
for(int j = 0; j < 4; j++)
{
t = fol[cur][j];
if(searchDist[cur] + dist[cur][j] < searchDist[t])
{
searchDist[t] = searchDist[cur] + dist[cur][j];
}
}
}
FOUND:
sprintf(buffer, "noting exit %d towards undiscovered crossroads #%d at distance %lf", i, cur, searchDist[cur]);
Log(buffer);
if(searchDist[cur] < min)
{
min = searchDist[cur];
rsl = i;
Log(" (temporarily best)");
}
Log("\n");
}
return rsl;
}
};
int main()
{
Navigator n;
n.initialize();
Point p;
int dir;
double dist;
while(scanf("%lf%lf%d%lf", &(p.x), &(p.y), &dir, &dist), n.navigate(p, dir, dist) != -2);
return 0;
}