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:
parent
292ebed18d
commit
e60c0b26eb
@ -1,3 +1,5 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -189,13 +191,26 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
struct Move {
|
struct Move {
|
||||||
from: Point2d,
|
from: Point2d,
|
||||||
dir: Vec2d,
|
dir: Vec2d,
|
||||||
prio: i32,
|
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
|
where
|
||||||
R: Rng,
|
R: Rng,
|
||||||
{
|
{
|
||||||
@ -232,7 +247,7 @@ fn main() -> std::io::Result<()> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let mut board = Board(Vec::new());
|
let mut board = Board(Vec::new());
|
||||||
let mut open = Vec::new();
|
let mut open = BinaryHeap::new();
|
||||||
let mut last_drawn_minute = 99;
|
let mut last_drawn_minute = 99;
|
||||||
loop {
|
loop {
|
||||||
if open.is_empty() {
|
if open.is_empty() {
|
||||||
@ -281,7 +296,6 @@ fn main() -> std::io::Result<()> {
|
|||||||
let work = arg.max(1);
|
let work = arg.max(1);
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
while !open.is_empty() && count < work {
|
while !open.is_empty() && count < work {
|
||||||
open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb));
|
|
||||||
let Move {
|
let Move {
|
||||||
from: p0,
|
from: p0,
|
||||||
dir: dir0,
|
dir: dir0,
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::io::stdout;
|
use std::io::stdout;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
@ -191,13 +193,26 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
struct Move {
|
struct Move {
|
||||||
from: Point2d,
|
from: Point2d,
|
||||||
dir: Vec2d,
|
dir: Vec2d,
|
||||||
prio: i32,
|
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
|
where
|
||||||
R: Rng,
|
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 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 board = Board(Vec::new());
|
||||||
let mut open = Vec::new();
|
let mut open = BinaryHeap::new();
|
||||||
let mut last_drawn_minute = 99;
|
let mut last_drawn_minute = 99;
|
||||||
loop {
|
loop {
|
||||||
if open.is_empty() {
|
if open.is_empty() {
|
||||||
@ -270,7 +285,6 @@ fn main() -> std::io::Result<()> {
|
|||||||
let work = arg.max(1);
|
let work = arg.max(1);
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
while !open.is_empty() && count < work {
|
while !open.is_empty() && count < work {
|
||||||
open.sort_unstable_by(|Move { prio: pa, .. }, Move { prio: pb, .. }| pa.cmp(pb));
|
|
||||||
let Move {
|
let Move {
|
||||||
from: p0,
|
from: p0,
|
||||||
dir: dir0,
|
dir: dir0,
|
||||||
|
@ -2,7 +2,7 @@ use std::ops::Add;
|
|||||||
|
|
||||||
use crate::vec2d::Vec2d;
|
use crate::vec2d::Vec2d;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub struct Point2d {
|
pub struct Point2d {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||||
pub struct Vec2d {
|
pub struct Vec2d {
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
|
Loading…
Reference in New Issue
Block a user