Use PositionType for position
This commit is contained in:
parent
2e9c699600
commit
207a0436d8
4 changed files with 283 additions and 194 deletions
|
|
@ -6,7 +6,7 @@ use crate::priority_queue::BinaryHeap;
|
|||
use colored::{Color, Colorize};
|
||||
use std::fmt::Display;
|
||||
|
||||
use self::common::Position;
|
||||
use self::common::{Position, PositionType};
|
||||
|
||||
pub mod brute_force;
|
||||
pub mod common;
|
||||
|
|
@ -69,17 +69,17 @@ impl Problem {
|
|||
for (i, path) in self.path.iter().enumerate() {
|
||||
if i != without {
|
||||
for p in path {
|
||||
self.map.get_mut(p.x, p.y).wheight += 50.0;
|
||||
self.map.get_mut(p.x as usize, p.y as usize).wheight += 50.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for p in &self.start {
|
||||
self.map.get_mut(p.x, p.y).wheight += 200.0;
|
||||
self.map.get_mut(p.x as usize, p.y as usize).wheight += 200.0;
|
||||
}
|
||||
|
||||
for p in &self.end {
|
||||
self.map.get_mut(p.x, p.y).wheight += 200.0;
|
||||
self.map.get_mut(p.x as usize, p.y as usize).wheight += 200.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -124,13 +124,21 @@ impl Display for Problem {
|
|||
write!(f, "{:1$}", y, height_digits as usize)?;
|
||||
|
||||
for x in 0..self.map.width {
|
||||
if let Some(i) = self.start.iter().position(|p| p == &Position::new(x, y)) {
|
||||
if let Some(i) = self
|
||||
.start
|
||||
.iter()
|
||||
.position(|p| p == &Position::new(x as PositionType, y as PositionType))
|
||||
{
|
||||
write!(f, "{}", "s".color(COLORS[i]))?;
|
||||
} else if let Some(i) = self.end.iter().position(|p| p == &Position::new(x, y)) {
|
||||
} else if let Some(i) = self
|
||||
.end
|
||||
.iter()
|
||||
.position(|p| p == &Position::new(x as PositionType, y as PositionType))
|
||||
{
|
||||
write!(f, "{}", "t".color(COLORS[i]))?;
|
||||
} else if let Some((i, p)) = self.path.iter().enumerate().find_map(|(i, v)| {
|
||||
v.iter()
|
||||
.position(|p| p == &Position::new(x, y))
|
||||
.position(|p| p == &Position::new(x as PositionType, y as PositionType))
|
||||
.map(|j| (i, j))
|
||||
}) {
|
||||
if self.path[i][p].x < self.path[i][p + 1].x {
|
||||
|
|
@ -164,19 +172,24 @@ struct MapInternal<'a> {
|
|||
|
||||
impl<'a> MapInternal<'a> {
|
||||
fn get_direction(&self, pos: Position) -> [Option<Position>; 4] {
|
||||
let min = Position::new(0, 0);
|
||||
let max = Position::new(
|
||||
self.map.width as PositionType,
|
||||
self.map.height as PositionType,
|
||||
);
|
||||
[
|
||||
(pos.x > 0)
|
||||
.then_some((pos.x.saturating_sub(1), pos.y))
|
||||
.map(|(x, y)| Position::new(x, y)),
|
||||
(pos.x < self.map.width - 1)
|
||||
.then_some((pos.x + 1, pos.y))
|
||||
.map(|(x, y)| Position::new(x, y)),
|
||||
(pos.y > 0)
|
||||
.then_some((pos.x, pos.y.saturating_sub(1)))
|
||||
.map(|(x, y)| Position::new(x, y)),
|
||||
(pos.y < self.map.height - 1)
|
||||
.then_some((pos.x, pos.y + 1))
|
||||
.map(|(x, y)| Position::new(x, y)),
|
||||
Position::new(pos.x - 1, pos.y)
|
||||
.in_range(&min, &max)
|
||||
.copied(),
|
||||
Position::new(pos.x + 1, pos.y)
|
||||
.in_range(&min, &max)
|
||||
.copied(),
|
||||
Position::new(pos.x, pos.y - 1)
|
||||
.in_range(&min, &max)
|
||||
.copied(),
|
||||
Position::new(pos.x, pos.y + 1)
|
||||
.in_range(&min, &max)
|
||||
.copied(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
@ -185,7 +198,7 @@ impl<'a> WheightedGraph for MapInternal<'a> {
|
|||
type Node = Position;
|
||||
|
||||
fn num_edges(&self, node: &Self::Node) -> usize {
|
||||
if self.map.get(node.x, node.y).blocked {
|
||||
if self.map.get(node.x as usize, node.y as usize).blocked {
|
||||
0
|
||||
} else {
|
||||
let t = self.get_direction(*node);
|
||||
|
|
@ -202,7 +215,7 @@ impl<'a> WheightedGraph for MapInternal<'a> {
|
|||
t.iter()
|
||||
.flatten()
|
||||
.nth(num)
|
||||
.map(|&p| (p, self.map.get(p.x, p.y).wheight))
|
||||
.map(|&p| (p, self.map.get(p.x as usize, p.y as usize).wheight))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue