mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Use Dijkstra instead of A*
The available distance heuristic doesn't save enough steps to offset its overhead.
This commit is contained in:
@@ -69,13 +69,13 @@ impl Map {
|
|||||||
|
|
||||||
pub fn shortest_path(&self, start: Point, end: Point) -> u32 {
|
pub fn shortest_path(&self, start: Point, end: Point) -> u32 {
|
||||||
let mut todo = BinaryHeap::new();
|
let mut todo = BinaryHeap::new();
|
||||||
todo.push(Reverse((Self::manhattan(start, end), 0, start)));
|
todo.push(Reverse((0, start)));
|
||||||
|
|
||||||
let mut visited = vec![false; self.data.len()];
|
let mut visited = vec![false; self.data.len()];
|
||||||
|
|
||||||
let height = self.height() as i32;
|
let height = self.height() as i32;
|
||||||
|
|
||||||
while let Some(Reverse((_, distance, pos))) = todo.pop() {
|
while let Some(Reverse((distance, pos))) = todo.pop() {
|
||||||
if pos == end {
|
if pos == end {
|
||||||
return distance;
|
return distance;
|
||||||
}
|
}
|
||||||
@@ -106,9 +106,8 @@ impl Map {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let new_distance = distance + self.data[index] as u32;
|
let new_distance = distance + self.data[index] as u32;
|
||||||
let new_guess = Self::manhattan(new, end) + new_distance;
|
|
||||||
|
|
||||||
todo.push(Reverse((new_guess, new_distance, new)));
|
todo.push(Reverse((new_distance, new)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,10 +115,6 @@ impl Map {
|
|||||||
panic!("No route found from {:?} to {:?}", start, end);
|
panic!("No route found from {:?} to {:?}", start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn manhattan((xa, ya): Point, (xb, yb): Point) -> u32 {
|
|
||||||
(xa - xb).abs() as u32 + (ya - yb).abs() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn height(&self) -> usize {
|
pub fn height(&self) -> usize {
|
||||||
self.data.len() / self.width
|
self.data.len() / self.width
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user