-
Notifications
You must be signed in to change notification settings - Fork 0
/
flood_fill.cr
28 lines (24 loc) · 903 Bytes
/
flood_fill.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Implementation of Flood Fill
# ([read more](https://en.wikipedia.org/wiki/Flood_fill)).
#
# It receives a BattleSnake::Point *a* and a BattleSnake::Context *context* to
# start off a Flood Fill and returns a Set(BattleSnake::Point) with all the
# points reachable to that area on the board
def Strategy::Utils.flood_fill(a : BattleSnake::Point, context : BattleSnake::Context)
area = Set(BattleSnake::Point).new
queue = [] of BattleSnake::Point
current = a
loop do
# Check all valid moves from current Point
context.valid_moves(current)[:neighbors].each_value.each do |point|
# Skip if already in area or in queue to be processed
next unless area.index { |p| (p <=> point).zero? }.nil?
next unless queue.index { |p| (p <=> point).zero? }.nil?
queue.push(point)
end
break if queue.empty?
current = queue.pop
area.add(current)
end
area
end