Lecture 4: Floodfill - UCLA IEEE Micromouse

Lecture 5: Floodfill
Floodfill Overview
• First assume that there are no walls
• Give each cell a distance from the goal. The goal has distance 0.
• Repeat until we reach the goal:
• Go to the neighboring cell with the smallest distance to the goal
• Run the update distances algorithm (see later slide)
Example
Maze Representation
• Each cell has a distance (0-252 for a 16 x 16 maze) and
• Each cell can be represented by 4 walls or 2 walls
• If you have 4 walls for each cell, then you need to update 2 places on
discovering a new wall.
• If you have 2 walls for each cell, then you need an extra “dummy” row and
column.
• You may also want to have a “visited” bit for speed runs.
• If you use the “visited” bit, then you don’t need to run the Update
Distances algorithm on cells that you have already visited, meaning
you can go faster on them.
Two Options For Maze Representation
+-----+-----+-----+
| N | N | N |
|W
E|W
E|W
E|
| S | S | S |
+-----+-----+-----+
| N | N | N |
|W
E|W
E|W
E|
| S | S | S |
+-----+-----+-----+
| N | N | N |
|W
E|W
E|W
E|
| S | S | S |
+-----+-----+-----+
3x3 Maze with
duplicate walls
+-----+-----+-----+-----+
|
|
|
|
|
|W X |W X |W X |W X |
| S | S | S | S |
+-----+-----+-----+-----+
|
|
|
|
|
|W
|W
|W
|W X |
| S | S | S | S |
+-----+-----+-----+-----+
|
|
|
|
|
|W
|W
|W
|W X |
| S | S | S | S |
+-----+-----+-----+-----+
|
|
|
|
|
|W
|W
|W
|W X |
| S | S | S | S |
+-----+-----+-----+-----+
3x3 Maze with Dummy row/column
X = dummy cell
Print maze function
• It is essential for debugging to be able to print out your maze
• Should look something like this:
+---+---+---+
|*5 *4 V3 |
+---+---+
+
| 0
1
2 |
+---+---+---+
• Use ‘+’ for posts. Use “---” for north/south walls. Use ‘|’ (pipe) for
east/west walls. Use ‘*’ for visited. Use <, >, ^, V for the direction of the
mouse.
• If you display distances in base 10, then you will need to do something else
when you have 3 digit distances.
The System Stack
• Space is allocated for each
function call
• de-allocated for function return
• Where local variables are stored
• Only 1 kB max by default in our
project files
• 1 MB by default on Windows
• 8 MB by default on Linux
• Recursion and large local arrays
are bad on embedded systems
due to the limited memory
Dynamic Memory Allocation
• Allocate memory at runtime on System Heap
• malloc and free (c standard library)
• new and delete (c++)
• Bad on embedded systems
• Takes an unpredictable amount of time
• Fragmentation may require moving arrays of data
• System Heap is small, similar to System Stack
• Makes your code more complicated and buggy
• Memory leaks are more difficult to detect
• No OS to kill your process, tell you what happened
Update Distances Algorithm
• Use a stack method instead of recursion
• Have stack S as a global variable (should be statically sized, not dynamic)
• This algorithm runs every time a new cell is visited by the mouse:
If there are any new walls discovered:
Make sure S is empty
Push current cell and cells adjacent to new walls onto S
While S is not empty:
currCell = pop(S)
correctDist = min(all open neighbor distances) + 1
if currCell.dist != correctDist and currCell.dist != 0:
currCell.dist = correctDist
push all open neighbors to S
Tips
• Run your Update Distances algorithm as soon as your mouse enters the
cell, not when it reaches the center of the cell.
• You can use Green’s maze generation excel sheet to create test mazes to
run your algorithm.
• For example “bool readLeftWall(), bool readRightWall(), bool readFrontWall()” will
read from Green’s array instead of the mouse’s IR sensors.
• The stack size for the update distances algorithm should be around 512.
• It is possible to add a cell more than once, therefore 255 may not be enough.