@@ -2,8 +2,9 @@ use std::collections::HashSet;
2
2
3
3
use crate :: grid:: { Cell , Grid , Point } ;
4
4
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 ( ) ;
7
8
let mut open_list: HashSet < Point > = HashSet :: new ( ) ;
8
9
9
10
for x in 0 ..( grid. width as i8 ) {
@@ -43,7 +44,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
43
44
} ;
44
45
45
46
if !visited. contains ( & neighbour)
46
- && ( free . contains ( & neighbour) || !grid. is_inside ( & neighbour) )
47
+ && ( free_cells . contains ( & neighbour) || !grid. is_inside ( & neighbour) )
47
48
{
48
49
visited. insert ( neighbour) ;
49
50
exit_count += 1 ;
@@ -67,7 +68,7 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
67
68
68
69
if !visited. contains ( & neighbour)
69
70
&& !visited. contains ( & corner)
70
- && ( free . contains ( & corner) || !grid. is_inside ( & corner) )
71
+ && ( free_cells . contains ( & corner) || !grid. is_inside ( & corner) )
71
72
{
72
73
visited. insert ( neighbour) ;
73
74
visited. insert ( corner) ;
@@ -81,25 +82,29 @@ pub fn get_free_cell(grid: &Grid, walkable: Cell) -> HashSet<Point> {
81
82
} ;
82
83
83
84
if has_enough_free_exits {
84
- free . insert ( p) ;
85
+ free_cells . insert ( p) ;
85
86
86
87
for dir in directions {
87
88
let neighbour = Point {
88
89
x : p. x + dir. x ,
89
90
y : p. y + dir. y ,
90
91
} ;
91
92
92
- if !free . contains ( & neighbour)
93
+ if !free_cells . contains ( & neighbour)
93
94
&& grid. is_inside ( & neighbour)
94
95
&& grid. get_cell ( & neighbour) <= walkable
95
96
{
96
97
open_list. insert ( neighbour) ;
97
98
}
98
99
}
100
+ } else {
101
+ one_way_cells. insert ( p) ;
99
102
}
100
103
}
101
104
102
- free
105
+ one_way_cells. retain ( |p| !free_cells. contains ( & p) ) ;
106
+
107
+ ( free_cells, one_way_cells)
103
108
}
104
109
105
110
#[ test]
@@ -108,7 +113,7 @@ fn it_should_collect_free_cell() {
108
113
109
114
grid. set_cell ( & Point { x : 1 , y : 1 } , Cell :: Color2 ) ;
110
115
111
- let free_cells = get_free_cell ( & grid, Cell :: Color1 ) ;
116
+ let ( free_cells, _ ) = get_free_cell ( & grid, Cell :: Color1 ) ;
112
117
113
118
assert_eq ! (
114
119
free_cells,
0 commit comments