Browse Source

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.
master
Juergen Stuber 2 years ago
parent
commit
e60c0b26eb
4 changed files with 36 additions and 8 deletions
  1. +17
    -3
      src/bin/dualmaze/main.rs
  2. +17
    -3
      src/bin/maze/main.rs
  3. +1
    -1
      src/point2d.rs
  4. +1
    -1
      src/vec2d.rs

+ 17
- 3
src/bin/dualmaze/main.rs 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,


+ 17
- 3
src/bin/maze/main.rs 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,


+ 1
- 1
src/point2d.rs 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,


+ 1
- 1
src/vec2d.rs 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,


Loading…
Cancel
Save