@@ -2,8 +2,9 @@ use std::collections::HashSet;
22
33use crate :: grid:: { Cell , Grid , Point } ;
44
5- pub fn get_free_cell ( grid : & Grid , walkable : Cell ) -> HashSet < Point > {
6- let mut free: HashSet < Point > = HashSet :: new ( ) ;
5+ pub fn get_free_cell ( grid : & Grid , walkable : Cell ) -> ( HashSet < Point > , HashSet < Point > ) {
6+ let mut free_cells: HashSet < Point > = HashSet :: new ( ) ;
7+ let mut one_way_cells: HashSet < Point > = HashSet :: new ( ) ;
78 let mut open_list: HashSet < Point > = HashSet :: new ( ) ;
89
910 for x in 0 ..( grid. width as i8 ) {
@@ -43,7 +44,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
4344 } ;
4445
4546 if !visited. contains ( & neighbour)
46- && ( free . contains ( & neighbour) || !grid. is_inside ( & neighbour) )
47+ && ( free_cells . contains ( & neighbour) || !grid. is_inside ( & neighbour) )
4748 {
4849 visited. insert ( neighbour) ;
4950 exit_count += 1 ;
@@ -67,7 +68,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
6768
6869 if !visited. contains ( & neighbour)
6970 && !visited. contains ( & corner)
70- && ( free . contains ( & corner) || !grid. is_inside ( & corner) )
71+ && ( free_cells . contains ( & corner) || !grid. is_inside ( & corner) )
7172 {
7273 visited. insert ( neighbour) ;
7374 visited. insert ( corner) ;
@@ -81,25 +82,29 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
8182 } ;
8283
8384 if has_enough_free_exits {
84- free . insert ( p) ;
85+ free_cells . insert ( p) ;
8586
8687 for dir in directions {
8788 let neighbour = Point {
8889 x : p. x + dir. x ,
8990 y : p. y + dir. y ,
9091 } ;
9192
92- if !free . contains ( & neighbour)
93+ if !free_cells . contains ( & neighbour)
9394 && grid. is_inside ( & neighbour)
9495 && grid. get_cell ( & neighbour) <= walkable
9596 {
9697 open_list. insert ( neighbour) ;
9798 }
9899 }
100+ } else {
101+ one_way_cells. insert ( p) ;
99102 }
100103 }
101104
102- free
105+ one_way_cells. retain ( |p| !free_cells. contains ( & p) ) ;
106+
107+ ( free_cells, one_way_cells)
103108}
104109
105110#[ test]
@@ -108,7 +113,7 @@ fn it_should_collect_free_cell() {
108113
109114 grid. set_cell ( & Point { x : 1 , y : 1 } , Cell :: Color2 ) ;
110115
111- let free_cells = get_free_cell ( & grid, Cell :: Color1 ) ;
116+ let ( free_cells, _ ) = get_free_cell ( & grid, Cell :: Color1 ) ;
112117
113118 assert_eq ! (
114119 free_cells,
0 commit comments