Pseudocode for Breadth-First Scanning
This code uses a trick to not use extra space, marking nodes to be visited as in component -2 and actually assigning them to the current component when they are actually visited.
# component(i) denotes the
# component that node i is in
1 function flood_fill(new_component)
2 do
3 num_visited = 0
4 for all nodes i
5 if component(i) = -2
6 num_visited = num_visited 1
7 component(i) = new_component
8 for all neighbors j of node i
9 if component(j) = nil
10 component(j) = -2
11 until num_visited = 0
12 function find_components
13 num_components = 0
14 for all nodes i
15 component(node i) = nil
16 for all nodes i
17 if component(node i) is nil
18 num_components =
num_components 1
19 component(i) = -2
20 flood_fill(component
num_components)
Running time of this algorithm is O(N 2), where N is the numbers of nodes. Every edge is traversed twice (once for each end-point), and each node is only marked once.
web:
1. If the color of node is not equal to target-color, return.
2. Set the color of node to replacement-color.
3. Perform Flood-fill (one step to the west of node,target-color, replacement-color).
Perform Flood-fill (one step to the east of node,target-color, replacement-color).
Perform Flood-fill (one step to the north of node,target-color, replacement-color).
Perform Flood-fill (one step to the south of node,target-color, replacement-color).
4. Return.
1. Set Q to the empty queue.
2. If the color of node is not equal to target-color, return.
3. Add node to the end of Q.
4. While "Q" is not empty:
5. Set "n" equal to the first element of "Q"
6. If the color of n is equal to target-color, set the color of n to replacement-color.
7. Remove first element from "Q"
8. If the color of the node to the west of n is target-color, set the color of that node to replacement-color, add that node to the end of Q.
9. If the color of the node to the east of n is target-color, set the color of that node to replacement-color, add that node to the end of Q.
10. If the color of the node to the north of n istarget-color, set the color of that node to replacement-color, add that node to the end of Q.
11. If the color of the node to the south of n istarget-color, set the color of that node to replacement-color, add that node to the end of Q.
12. Return.
{
if(oldColor == newColor) return;
if(screenBuffer[x][y] != oldColor) return;
int y1;
//draw current scanline from start position to the top
y1 = y;
while(y1 < h && screenBuffer[x][y1] == oldColor)
{
screenBuffer[x][y1] = newColor;
y1 ;
}
//draw current scanline from start position to the bottom
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == oldColor)
{
screenBuffer[x][y1] = newColor;
y1--;
}
//test for new scanlines to the left
y1 = y;
while(y1 < h && screenBuffer[x][y1] == newColor)
{
if(x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
floodFillScanline(x - 1, y1, newColor, oldColor);
}
y1 ;
}
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == newColor)
{
if(x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
floodFillScanline(x - 1, y1, newColor, oldColor);
}
y1--;
}
//test for new scanlines to the right
y1 = y;
while(y1 < h && screenBuffer[x][y1] == newColor)
{
if(x < w - 1 && screenBuffer[x 1][y1] == oldColor)
{
floodFillScanline(x 1, y1, newColor, oldColor);
}
y1 ;
}
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == newColor)
{
if(x < w - 1 && screenBuffer[x 1][y1] == oldColor)
{
floodFillScanline(x 1, y1, newColor, oldColor);
}
y1--;
}
}
{
if(oldColor == newColor) return;
emptyStack();
int y1;
bool spanLeft, spanRight;
if(!push(x, y)) return;
while(pop(x, y))
{
y1 = y;
while(y1 >= 0 && screenBuffer[x][y1] == oldColor) y1--;
y1 ;
spanLeft = spanRight = 0;
while(y1 < h && screenBuffer[x][y1] == oldColor )
{
screenBuffer[x][y1] = newColor;
if(!spanLeft && x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
if(!push(x - 1, y1)) return;
spanLeft = 1;
}
else if(spanLeft && x > 0 && screenBuffer[x - 1][y1] != oldColor)
{
spanLeft = 0;
}// 写这一部分是防止因为同一列上有断开的段而造成的可能的“没填充”
if(!spanRight && x < w - 1 && screenBuffer[x 1][y1] == oldColor)
{
if(!push(x 1, y1)) return;
spanRight = 1;
}
else if(spanRight && x < w - 1 && screenBuffer[x 1][y1] != oldColor)
{
spanRight = 0;
} //写这一部分是防止因为同一列上有断开的段而造成的可能的“没填充”
y1 ;
}
}
}