Skip to content

Commit c983626

Browse files
committed
Add 2d array
1 parent 82502ba commit c983626

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod models;
22
pub mod parse;
3-
pub mod solve;
3+
pub mod solve;
4+
pub mod utils;

src/solve.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::models::*;
2+
use crate::utils::Matrix;
23

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

2122
for p in &map_points {
22-
passed[p.y][p.x] = false;
23-
valid[p.y][p.x] = true;
23+
passed.set(p, false);
24+
valid.set(p, true);
2425
remaining += 1;
2526
}
2627

2728
for o in &task.obstacles {
2829
for p in o.enumerate_points().iter() {
29-
if p.y < height && p.x < width && valid[p.y][p.x] {
30-
valid[p.y][p.x] = false;
31-
passed[p.y][p.x] = true;
30+
if p.y < height && p.x < width && valid.get(p) {
31+
valid.set(p, false);
32+
passed.set(p, true);
3233
remaining -= 1;
3334
}
3435
}
@@ -42,25 +43,25 @@ pub fn solve_small(task: Task) -> Vec<Command> {
4243
];
4344
let mut res = Vec::new();
4445
let mut cp = task.initial;
45-
if !passed[cp.y][cp.x] {
46-
passed[cp.y][cp.x] = true;
46+
if !passed.get(&cp) {
47+
passed.set(&cp, true);
4748
remaining -= 1;
4849
}
4950
while remaining > 0 {
50-
let mut data = vec![vec![None; width]; height];
51+
let mut data = Matrix::new(width, height, None);
5152
let mut queue = VecDeque::new();
5253
queue.push_back(cp);
53-
data[cp.y][cp.x] = Some(Command::Noop);
54+
data.set(&cp, Some(Command::Noop));
5455
let mut reached = false;
5556
while let Some(c) = queue.pop_front() {
56-
if !passed[c.y][c.x] {
57-
passed[c.y][c.x] = true;
57+
if !passed.get(&c) {
58+
passed.set(&c, true);
5859
remaining -= 1;
5960

6061
let mut local_cmds = Vec::new();
6162
let mut iter = c;
6263
while iter != cp {
63-
let cmd = data[iter.y][iter.x].unwrap();
64+
let cmd = data.get(&iter).unwrap();
6465
local_cmds.push(cmd);
6566
iter = iter.revert_with(cmd).unwrap();
6667
}
@@ -74,12 +75,8 @@ pub fn solve_small(task: Task) -> Vec<Command> {
7475
moves.shuffle(&mut rng);
7576
for m in &moves {
7677
if let Some(nc) = c.move_with(*m) {
77-
if nc.x < width
78-
&& nc.y < height
79-
&& data[nc.y][nc.x].is_none()
80-
&& valid[nc.y][nc.x]
81-
{
82-
data[nc.y][nc.x] = Some(*m);
78+
if nc.x < width && nc.y < height && data.get(&nc).is_none() && valid.get(&nc) {
79+
data.set(&nc, Some(*m));
8380
queue.push_back(nc);
8481
}
8582
}

src/utils.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::models::Point;
2+
3+
pub struct Matrix<T: Copy> {
4+
width: usize,
5+
inner: Vec<T>,
6+
}
7+
8+
impl<T: Copy> Matrix<T> {
9+
pub fn new(width: usize, height: usize, init: T) -> Matrix<T> {
10+
let n = width * height;
11+
Matrix {
12+
width,
13+
inner: vec![init; n],
14+
}
15+
}
16+
17+
pub fn get(&self, p: &Point) -> T {
18+
self.inner[p.y * self.width + p.x]
19+
}
20+
21+
pub fn get_mut(&mut self, p: &Point) -> &mut T {
22+
&mut self.inner[p.y * self.width + p.x]
23+
}
24+
25+
pub fn set(&mut self, p: &Point, value: T) {
26+
*self.get_mut(p) = value
27+
}
28+
}

0 commit comments

Comments
 (0)