Fix node skipping by computing the accumulated proximity

This commit is contained in:
Kerollmops 2020-06-12 14:08:10 +02:00
parent 37a48489da
commit d02c5cb023
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 12 additions and 5 deletions

View File

@ -35,6 +35,8 @@ enum Node {
layer: usize,
// The position where this node is located.
position: u32,
// The total accumulated proximity until this node, used for skipping nodes.
acc_proximity: u32,
},
}
@ -45,17 +47,19 @@ impl Node {
fn successors(&self, positions: &[Vec<u32>], best_proximity: u32) -> Vec<(Node, u32)> {
match self {
Node::Uninit => {
positions[0].iter().map(|p| (Node::Init { layer: 0, position: *p }, 0)).collect()
positions[0].iter().map(|p| {
(Node::Init { layer: 0, position: *p, acc_proximity: 0 }, 0)
}).collect()
},
// We reached the highest layer
n @ Node::Init { .. } if n.is_complete(positions) => vec![],
Node::Init { layer, position } => {
Node::Init { layer, position, acc_proximity } => {
let layer = layer + 1;
positions[layer].iter().filter_map(|p| {
let proximity = positions_proximity(*position, *p);
let node = Node::Init { layer, position: *p };
let node = Node::Init { layer, position: *p, acc_proximity: acc_proximity + proximity };
// We do not produce the nodes we have already seen in previous iterations loops.
if node.is_complete(positions) && proximity < best_proximity {
if node.is_complete(positions) && acc_proximity + proximity < best_proximity {
None
} else {
Some((node, proximity))

View File

@ -121,7 +121,10 @@ impl Index {
for (proximity, mut positions) in BestProximity::new(positions) {
// TODO we must ignore positions paths that gives nothing
if words.len() > 1 && proximity == 0 { continue }
if (proximity as usize) < words.len() - 1 {
eprintln!("Skipping too short proximities of {}.", proximity);
continue
}
positions.sort_unstable();