Skip to content

Commit db9a825

Browse files
committed
Add FloodFill Algorithm
1 parent 36e74ba commit db9a825

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

algorithm/category.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"etc": {
5858
"name": "Uncategorized",
5959
"list": {
60+
"flood_fill": "Flood Fill"
6061
}
6162
}
6263
}

algorithm/etc/flood_fill/desc.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Flood Fill": "Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array",
3+
"References": [
4+
"<a href='https://en.wikipedia.org/wiki/Flood_fill'>Wikipedia</a>"
5+
],
6+
"files": {
7+
"flood_fill": ""
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function FloodFill(i, j, oldColor, newColor) {
2+
3+
if (i < 0 || i >= G.length || j < 0 || j >= G[i].length) return;
4+
if (G[i][j] != oldColor) return;
5+
6+
// set the color of node to newColor
7+
G[i][j] = newColor;
8+
9+
tracer._select(i, j)._wait();
10+
tracer._notify(i, j, G[i][j])._wait();
11+
12+
// next step four-way
13+
FloodFill(i + 1, j, oldColor, newColor);
14+
FloodFill(i - 1, j, oldColor, newColor);
15+
FloodFill(i, j + 1, oldColor, newColor);
16+
FloodFill(i, j - 1, oldColor, newColor);
17+
18+
tracer._denotify(i, j);
19+
tracer._deselect(i, j)._wait();
20+
}
21+
22+
FloodFill(4, 4, '-', 'a');
23+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var tracer = new Array2DTracer ();
2+
var G = [
3+
['#', '#', '#', '#', '#', '#', '#', '#', '#'],
4+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
5+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
6+
['#', '-', '-', '#', '-', '-', '-', '-', '#'],
7+
['#', '#', '#', '-', '-', '-', '#', '#', '#'],
8+
['#', '-', '-', '-', '-', '#', '-', '-', '#'],
9+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
10+
['#', '-', '-', '-', '#', '-', '-', '-', '#'],
11+
['#', '#', '#', '#', '#', '#', '#', '#', '#']
12+
];
13+
tracer._setData(G);

0 commit comments

Comments
 (0)