Skip to content
Permalink
Browse files

Add 2d array

  • Loading branch information...
ichyo committed Jun 22, 2019
1 parent 82502ba commit c983626dc483b9a5e0a3478c624e1aa17ec91fdc
Showing with 47 additions and 21 deletions.
  1. +2 −1 src/lib.rs
  2. +17 −20 src/solve.rs
  3. +28 −0 src/utils.rs
@@ -1,3 +1,4 @@
pub mod models;
pub mod parse;
pub mod solve;
pub mod solve;
pub mod utils;
@@ -1,4 +1,5 @@
use crate::models::*;
use crate::utils::Matrix;

use rand::seq::SliceRandom;
use rand::thread_rng;
@@ -15,20 +16,20 @@ pub fn solve_small(task: Task) -> Vec<Command> {
let width = map_points.iter().map(|p| p.x).max().unwrap() + 1;
let height = map_points.iter().map(|p| p.y).max().unwrap() + 1;
let mut remaining = 0;
let mut passed = vec![vec![true; width]; height];
let mut valid = vec![vec![false; width]; height];
let mut passed = Matrix::new(width, height, true);
let mut valid = Matrix::new(width, height, false);

for p in &map_points {
passed[p.y][p.x] = false;
valid[p.y][p.x] = true;
passed.set(p, false);
valid.set(p, true);
remaining += 1;
}

for o in &task.obstacles {
for p in o.enumerate_points().iter() {
if p.y < height && p.x < width && valid[p.y][p.x] {
valid[p.y][p.x] = false;
passed[p.y][p.x] = true;
if p.y < height && p.x < width && valid.get(p) {
valid.set(p, false);
passed.set(p, true);
remaining -= 1;
}
}
@@ -42,25 +43,25 @@ pub fn solve_small(task: Task) -> Vec<Command> {
];
let mut res = Vec::new();
let mut cp = task.initial;
if !passed[cp.y][cp.x] {
passed[cp.y][cp.x] = true;
if !passed.get(&cp) {
passed.set(&cp, true);
remaining -= 1;
}
while remaining > 0 {
let mut data = vec![vec![None; width]; height];
let mut data = Matrix::new(width, height, None);
let mut queue = VecDeque::new();
queue.push_back(cp);
data[cp.y][cp.x] = Some(Command::Noop);
data.set(&cp, Some(Command::Noop));
let mut reached = false;
while let Some(c) = queue.pop_front() {
if !passed[c.y][c.x] {
passed[c.y][c.x] = true;
if !passed.get(&c) {
passed.set(&c, true);
remaining -= 1;

let mut local_cmds = Vec::new();
let mut iter = c;
while iter != cp {
let cmd = data[iter.y][iter.x].unwrap();
let cmd = data.get(&iter).unwrap();
local_cmds.push(cmd);
iter = iter.revert_with(cmd).unwrap();
}
@@ -74,12 +75,8 @@ pub fn solve_small(task: Task) -> Vec<Command> {
moves.shuffle(&mut rng);
for m in &moves {
if let Some(nc) = c.move_with(*m) {
if nc.x < width
&& nc.y < height
&& data[nc.y][nc.x].is_none()
&& valid[nc.y][nc.x]
{
data[nc.y][nc.x] = Some(*m);
if nc.x < width && nc.y < height && data.get(&nc).is_none() && valid.get(&nc) {
data.set(&nc, Some(*m));
queue.push_back(nc);
}
}
@@ -0,0 +1,28 @@
use crate::models::Point;

pub struct Matrix<T: Copy> {
width: usize,
inner: Vec<T>,
}

impl<T: Copy> Matrix<T> {
pub fn new(width: usize, height: usize, init: T) -> Matrix<T> {
let n = width * height;
Matrix {
width,
inner: vec![init; n],
}
}

pub fn get(&self, p: &Point) -> T {
self.inner[p.y * self.width + p.x]
}

pub fn get_mut(&mut self, p: &Point) -> &mut T {
&mut self.inner[p.y * self.width + p.x]
}

pub fn set(&mut self, p: &Point, value: T) {
*self.get_mut(p) = value
}
}

0 comments on commit c983626

Please sign in to comment.
You can’t perform that action at this time.