fn entry(grid: Vec<bool>, width: usize, _height: usize, x: usize, y: usize) -> Vec<bool>{
struct Pos {
x: usize,
y: usize
}
fn get_at(grid: &Vec<bool>, width:usize, pos: &Pos) -> bool {
return grid[pos.y*width+pos.x]
}
fn get_neighbours(grid: &Vec<bool>, width:usize, height:usize, pos:&Pos) -> Vec<Pos> {
let mut potential_neighbors: Vec<Pos> = Vec::new();
if pos.x>0 {
potential_neighbors.push(Pos{x:pos.x-1, y:pos.y})
}
if pos.x<width-1 {
potential_neighbors.push(Pos{x:pos.x+1, y:pos.y})
}
if pos.y>0 {
potential_neighbors.push(Pos{x:pos.x, y:pos.y-1})
}
if pos.y<height-1 {
potential_neighbors.push(Pos{x:pos.x, y:pos.y+1})
}
let mut yes_neighbours: Vec<Pos> = Vec::new();
for neighbor in potential_neighbors {
if ! get_at(grid, width, &neighbor) {
yes_neighbours.push(neighbor)
}
}
return yes_neighbours
}
let pos = Pos{x:x, y:y};
let mut staccc:Vec<Pos> = Vec::new();
let mut new_grid = grid.clone();
if !get_at(&grid, width, &pos) {
staccc.push(pos);
while !staccc.is_empty() {
let nex = staccc.pop().unwrap();
new_grid[nex.y*width + nex.x] = true;
let neis = get_neighbours(&new_grid, width, _height, &nex);
for nei in neis {
staccc.push(nei)
}
}
}
return new_grid
}
post a comment