59 return x == other.
x and y == other.
y;
73 return abs(f.x - t.x) +
abs(f.y - t.y);
78 cout <<
"=== IDA* (Iterative Deepening A*) Example ===" << endl << endl;
84 for (
int i = 0; i < grid_size; ++i)
87 for (
int j = 0; j < grid_size; ++j)
92 auto set_obstacle = [&](
int y,
int x,
bool val) {
97 auto get_obstacle = [&](
int y,
int x) ->
bool {
98 return obstacles.
nth(
y).nth(x);
101 for (
int i = 1; i <= 3; ++i) {
102 set_obstacle(1, i,
true);
103 set_obstacle(3, i,
true);
105 set_obstacle(2, 1,
true);
106 set_obstacle(2, 3,
true);
108 cout <<
"Grid-based pathfinding (5x5):" << endl;
109 cout <<
"S = Start, G = Goal, # = Obstacle, . = Free" << endl << endl;
111 for (
int y = 0;
y < grid_size; ++
y) {
112 for (
int x = 0; x < grid_size; ++x) {
113 if (x == 0
and y == 0)
115 else if (x == grid_size - 1
and y == grid_size - 1)
117 else if (get_obstacle(
y, x))
130 for (
int y = 0;
y < grid_size; ++
y) {
131 for (
int x = 0; x < grid_size; ++x) {
132 if (not get_obstacle(
y, x)) {
139 for (
int y = 0;
y < grid_size; ++
y) {
140 for (
int x = 0; x < grid_size; ++x) {
141 if (get_obstacle(
y, x))
144 int dx[] = {0, 0, 1, -1};
145 int dy[] = {1, -1, 0, 0};
147 for (
int d = 0; d < 4; ++d) {
151 if (nx >= 0
and nx < grid_size and ny >= 0
and ny < grid_size
152 and not get_obstacle(ny, nx)) {
153 auto src =
nodes.find(make_pair(x,
y));
154 auto tgt =
nodes.find(make_pair(nx, ny));
161 auto start =
nodes.find(make_pair(0, 0));
162 auto goal =
nodes.find(make_pair(grid_size - 1, grid_size - 1));
164 cout <<
"Running IDA* with Manhattan distance heuristic:" << endl;
169 auto cost = ida(g, start, goal, path);
171 if (cost == numeric_limits<int>::max()) {
172 cout <<
"No path found!" << endl;
174 cout <<
"Path found! Cost: " << cost << endl << endl;
176 cout <<
"Path coordinates: ";
179 auto & c = n->get_info();
180 cout <<
"(" << c.x <<
"," << c.y <<
") ";
183 cout << endl <<
"Path length: " << (
count - 1) <<
" moves" << endl;
186 cout << endl <<
"=== Performance ===" << endl
187 <<
"Time: O(b^d)" << endl
188 <<
"Space: O(d) [linear in depth]" << endl;
IDA* (Iterative Deepening A*) shortest path algorithm.
Core header for the Aleph-w library.
void insert(Dlink *node) noexcept
Insert node after this.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
Generic key-value map implemented on top of a binary search tree.
IDA* algorithm for memory-efficient shortest path search.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
void for_each_node(Operation op=Operation()) const
Execute an operation on each node of path.
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Type & nth(const size_t n)
Return the n-th item of the container.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Arc of graph implemented with double-linked adjacency lists.
bool operator==(const Coord &other) const
int operator()(typename GT::Node *from, typename GT::Node *to) const
Dynamic key-value map based on balanced binary search trees.
Generic graph and digraph implementations.