Use priority queue for next move to make in mazes.

This is the proper data structure to use.
No more of this regularly resorting a vector rubbish.
This commit is contained in:
Juergen Stuber 2018-12-03 20:20:04 +01:00
parent 292ebed18d
commit e60c0b26eb
4 changed files with 36 additions and 8 deletions

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::env::args;
use std::io::stdout;
use std::io::Write;
@ -189,13 +191,26 @@ impl Board {
}
}
#[derive(PartialEq, Eq)]
struct Move {
from: Point2d,
dir: Vec2d,
prio: i32,
}
fn add_move<R>(board: &Board, open: &mut Vec<Move>, rng: &mut R, from: Point2d, dir: Vec2d)
impl PartialOrd for Move {
fn partial_cmp(&self, other: &Move) -> Option<Ordering> {
self.prio.partial_cmp(&other.prio)
}
}
impl Ord for Move {
fn cmp(&self, other: &Move) -> Ordering {
self.prio.cmp(&other.prio)
}
}
fn add_move<R>(board: &Board, open: &mut BinaryHeap<Move>, rng: &mut R, from: Point2d, dir: Vec2d)
where
R: Rng,
{
@ -232,7 +247,7 @@ fn main() -> std::io::Result<()> {
);
let mut board = Board(Vec::new());
let mut open = Vec::new();
let mut open = BinaryHeap::new();
let mut last_drawn_minute = 99;
loop {
if open.is_empty() {
@ -281,7 +296,6 @@ fn main() -> std::io::Result<()> {
let work = arg.max(1);
let mut count = 0;
while !open.is_empty() && count < work {
open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb));
let Move {
from: p0,
dir: dir0,

View File

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::env::args;
use std::io::stdout;
use std::io::Write;
@ -191,13 +193,26 @@ impl Board {
}
}
#[derive(PartialEq, Eq)]
struct Move {
from: Point2d,
dir: Vec2d,
prio: i32,
}
fn add_move<R>(board: &Board, open: &mut Vec<Move>, rng: &mut R, from: Point2d, dir: Vec2d)
impl PartialOrd for Move {
fn partial_cmp(&self, other: &Move) -> Option<Ordering> {
self.prio.partial_cmp(&other.prio)
}
}
impl Ord for Move {
fn cmp(&self, other: &Move) -> Ordering {
self.prio.cmp(&other.prio)
}
}
fn add_move<R>(board: &Board, open: &mut BinaryHeap<Move>, rng: &mut R, from: Point2d, dir: Vec2d)
where
R: Rng,
{
@ -236,7 +251,7 @@ fn main() -> std::io::Result<()> {
let maze_mid = p2d((maze_size.x - 1) / 4 * 2 + 1, (maze_size.y - 1) / 4 * 2 + 1);
let mut board = Board(Vec::new());
let mut open = Vec::new();
let mut open = BinaryHeap::new();
let mut last_drawn_minute = 99;
loop {
if open.is_empty() {
@ -270,7 +285,6 @@ fn main() -> std::io::Result<()> {
let work = arg.max(1);
let mut count = 0;
while !open.is_empty() && count < work {
open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb));
let Move {
from: p0,
dir: dir0,

View File

@ -2,7 +2,7 @@ use std::ops::Add;
use crate::vec2d::Vec2d;
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Point2d {
pub x: i32,
pub y: i32,

View File

@ -1,6 +1,6 @@
use std::ops::Add;
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Vec2d {
pub x: i32,
pub y: i32,