aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzedarider <ymherklotz@gmail.com>2016-12-21 15:18:29 +0000
committerzedarider <ymherklotz@gmail.com>2016-12-21 15:18:29 +0000
commit61e0e87e90101aa565b088687785b6c73b9c5c58 (patch)
treec410fe951decaa9d5d4eb9cbec44d0410c41d914
parent2d2ba3f3db635bf72b56a8f311e0468e27e68deb (diff)
downloadA-star-algorithm-61e0e87e90101aa565b088687785b6c73b9c5c58.tar.gz
A-star-algorithm-61e0e87e90101aa565b088687785b6c73b9c5c58.zip
optimised and using array instead of vector
-rwxr-xr-xbin/mainbin356696 -> 356696 bytes
-rw-r--r--src/astar.cpp23
2 files changed, 13 insertions, 10 deletions
diff --git a/bin/main b/bin/main
index 449e64f..87f8790 100755
--- a/bin/main
+++ b/bin/main
Binary files differ
diff --git a/src/astar.cpp b/src/astar.cpp
index df2115b..21c518d 100644
--- a/src/astar.cpp
+++ b/src/astar.cpp
@@ -49,7 +49,11 @@ bool AStar::start_algorithm(int *curr_graph, const unsigned int& width, const un
bool AStar::start_algorithm() {
open_set.clear();
- find_start_end();
+ if(!find_start_end()) {
+ throw "Couldn't find start or end";
+ return false;
+ }
+ reset_closed_set();
open_set.push(start_node);
while(open_set.first() != end_node) {
Node current = open_set.pop();
@@ -100,7 +104,6 @@ bool AStar::find_start_end() {
} else if(graph[i] == 2) {
end_node.index = i;
}
- open_set.push(start_node);
Node n;
return !(start_node == n || end_node == n);
}
@@ -109,28 +112,28 @@ Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) {
Node n;
if(neighbour_num == 0 && n_in.index / graph_width > 0) {
- n.index -= graph_width;
+ n.index = n_in.index - graph_width;
path_length = PATHLENGTH;
} else if(neighbour_num == 1 && n_in.index % graph_width < graph_width - 1) {
- n.index += 1;
+ n.index = n_in.index + 1;
path_length = PATHLENGTH;
} else if(neighbour_num == 2 && n_in.index / graph_width < graph_height - 1) {
- n.index += graph_width;
+ n.index = n_in.index + graph_width;
path_length = PATHLENGTH;
} else if(neighbour_num == 3 && n_in.index % graph_width > 0) {
- n.index -= 1;
+ n.index = n_in.index - 1;
path_length = PATHLENGTH;
} else if(neighbour_num == 4 && n_in.index / graph_width > 0 && n_in.index % graph_width < graph_height - 1) {
- n.index -= graph_width + 1;
+ n.index = n_in.index - graph_width + 1;
path_length = DIAGLENGTH;
} else if(neighbour_num == 5 && n_in.index / graph_width < graph_height - 1 && n_in.index % graph_width < graph_width - 1) {
- n.index += graph_width + 1;
+ n.index = n_in.index + graph_width + 1;
path_length = DIAGLENGTH;
} else if(neighbour_num == 6 && n_in.index / graph_width < graph_height - 1 && n_in.index % graph_width > 0) {
- n.index -= graph_width - 1;
+ n.index = n_in.index - graph_width - 1;
path_length = DIAGLENGTH;
} else if(neighbour_num == 7 && n_in.index / graph_width > 0 && n_in.index % graph_width > 0) {
- n.index -= graph_width - 1;
+ n.index = n_in.index + graph_width - 1;
path_length = DIAGLENGTH;
}