-
Notifications
You must be signed in to change notification settings - Fork 13
/
catching-up.cpp
280 lines (227 loc) · 8.01 KB
/
catching-up.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
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
#include <bits/stdc++.h>
namespace env {
using Dims = std::pair<size_t, size_t>;
/*****************************************************************************/
class ICell
{
public:
enum State
{
EMPTY = 0,
WALL = 1 << 1
};
public:
ICell(uint x, uint y) noexcept : _x{x}, _y{y} {}
virtual ~ICell() noexcept = default;
virtual void clean(void) noexcept {}
auto x(void) const noexcept { return _x; }
auto y(void) const noexcept { return _y; }
bool hasState(int st) const noexcept { return _state & st; }
int getState(void) const noexcept { return _state; }
[[maybe_unused]] bool setState(int st) noexcept {
if ( st != _state ) {
_state = st;
return true;
}
return false;
}
[[maybe_unused]] bool addState(State st) noexcept {
if (!(_state & st)) {
_state |= st;
return true;
}
return false;
}
[[maybe_unused]] bool remState(State st) noexcept{
if (_state & st) {
_state &= ~st;
return true;
}
return false;
}
protected:
uint _x, _y;
int _state{ EMPTY };
};
/*****************************************************************************/
class AStarCell : public ICell
{
public:
AStarCell(uint x, uint y, AStarCell* parent = nullptr): ICell(x, y), _parent{parent} {}
virtual ~AStarCell() noexcept = default;
void clean(void) noexcept override {
_G = _H = 0;
_parent = nullptr;
}
uint getScore() const noexcept { return _G + _H; }
uint _G{ 0 }, _H{ 0 };
AStarCell* _parent;
};
/*****************************************************************************/
template<typename T, typename = std::enable_if_t<std::is_base_of_v<ICell, T>>>
class Graph
{
public:
Graph(size_t width = 10, size_t height = 10) noexcept
: _width{ width }
, _height{ height }
{
resize(width, height);
}
virtual ~Graph() noexcept = default;
[[maybe_unused]] void clean(void) noexcept
{
std::for_each(std::begin(_data), std::end(_data), [](auto& c) { c.clean(); });;
}
virtual void resize(size_t width, size_t height) noexcept
{
_width = width;
_height = height;
_data.clear();
_data.reserve(_width * _height);
for (size_t i{ 0 }; i < _height; ++i)
for (size_t j{ 0 }; j < _width; ++j)
_data.push_back(T(j, i));
}
auto getWidth(void) const noexcept { return _width; }
auto getHeight(void) const noexcept { return _height; }
Dims getSize(void) const noexcept { return { _width, _height }; }
T* cell(size_t i, size_t j) const noexcept
{
return (i < _width && j < _height) ? &_data.at(i + j * _width) : nullptr;
}
char cardinal(T* src, T* dst) const noexcept {
if ( nullptr == src || nullptr == dst || dst == src ) return ' ';
if ( src->x() == dst->x() )
return src->y() < dst->y() ? 'R' : 'L';
if ( src->y() == dst->y() )
return src->x() < dst->x() ? 'D' : 'U';
return ' ';
}
protected:
size_t _width, _height;
mutable std::vector<T> _data;
};
}
namespace algo {
template<typename T>
using HeuristicFunction = std::function<uint(T*, T*)>;
using CellsList = std::list<env::ICell*>;
using CellsSet = std::set<env::ICell*>;
/*****************************************************************************/
class Heuristic
{
public:
static uint manhattan(env::AStarCell* src, env::AStarCell* dst) noexcept {
auto delta{ getDelta(src, dst) };
return static_cast<uint>(10 * (delta.first + delta.second));
}
static uint euclidean(env::AStarCell* src, env::AStarCell* dst) noexcept {
auto delta{ getDelta(src, dst) };
return static_cast<uint>(10 * sqrt(pow(delta.first, 2) + pow(delta.second, 2)));
}
static uint octagonal(env::AStarCell* src, env::AStarCell* dst) noexcept {
auto delta{ getDelta(src, dst) };
return 10 * (delta.first + delta.second) + (-6) * std::min(delta.first, delta.second);
}
private:
static std::pair<int, int> getDelta(env::AStarCell* s, env::AStarCell* d) noexcept {
return { abs(static_cast<int>(s->x()) - static_cast<int>(d->x())),
abs(static_cast<int>(s->y()) - static_cast<int>(d->y())) };
}
};
/*****************************************************************************/
template<typename T>
class Impl
{
public:
Impl() noexcept: _heuristic{ std::bind(&Heuristic::euclidean, std::placeholders::_1, std::placeholders::_2) }
, _dirs{ 4 }
{}
virtual ~Impl() noexcept = default;
[[maybe_unused]] virtual T* run(env::Graph<T>* world, T* start, T* end) noexcept;
protected:
virtual bool _eligible(T* cell) noexcept {
return (nullptr == cell) ? false : !cell->hasState(env::ICell::WALL);
}
private:
env::Graph<T>* _world{ nullptr };
HeuristicFunction<T> _heuristic;
uint _dirs;
static const std::vector<std::pair<int, int>> DIRS;
};
template<typename T>
const std::vector<std::pair<int, int>> Impl<T>::DIRS{ { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 },
{ -1, -1 }, { 1, 1 }, { -1, 1 }, { 1, -1 } };
template < typename T>
T* Impl<T>::run(env::Graph<T>* world, T* start, T* end) noexcept {
_world = world;
if (nullptr == _world || nullptr == start || nullptr == end)
return nullptr;
env::AStarCell* cur{ nullptr };
CellsList open;
CellsSet closed;
open.push_back(start);
while (!std::empty(open)) {
auto it{ std::max_element(std::begin(open), std::end(open), [](auto* a, auto* b) {
return static_cast<env::AStarCell*>(a)->getScore() >= static_cast<env::AStarCell*>(b)->getScore();
}) };
cur = static_cast<env::AStarCell*>(*it);
if (end == cur)
break;
closed.insert(cur);
open.erase(it);
for (uint i{ 0 }; i < _dirs; ++i) {
auto neigh{ _world->cell(cur->x() + DIRS[i].first, cur->y() + DIRS[i].second) };
if (!_eligible(neigh) || closed.count(neigh))
continue;
uint totalCost{ cur->_G + ((i < 4) ? 10 : 14) };
if (std::end(open) == std::find(std::begin(open), std::end(open), neigh)) {
neigh->_parent = cur;
neigh->_G = totalCost;
neigh->_H = _heuristic(neigh, end);
open.push_back(neigh);
} else if (totalCost < neigh->_G) {
neigh->_parent = cur;
neigh->_G = totalCost;
}
}
}
if (cur != end)
return nullptr;
while (start != cur->_parent ) { cur = cur->_parent; }
return cur;
}
}
/*****************************************************************************/
int main()
{
int k;
std::cin >> k; std::cin.ignore();
env::Graph<env::AStarCell> graph;
algo::Impl<env::AStarCell> algo;
env::AStarCell* me{ nullptr }, *opp{ nullptr };
for ( auto i{ 0 }; i < 10; ++i ) {
std::string line;
getline(std::cin, line);
for ( auto j{ 0 }; j < 10; ++j ) {
auto cell{ graph.cell(i, j) };
switch ( line[j] ) {
case '*': cell->setState(env::ICell::WALL); break;
case 'P': me = cell; break;
case 'E': opp = cell; break;
default: break;
}
}
}
while (1) {
int ene_y, ene_x;
std::cin >> ene_y >> ene_x; std::cin.ignore();
graph.clean();
opp = graph.cell(ene_y, ene_x);
if ( auto nxtCell{ algo.run(&graph, me, opp) }; nullptr != nxtCell ) {
std::cout << graph.cardinal(me, nxtCell) << '\n';
me = nxtCell;
}
}
}