
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Data Structures and Types
- DSA - Array Data Structure
- Linked Lists
- DSA - Linked List Basics
- DSA - Doubly Linked List
- DSA - Circular Linked List
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Red Black Trees
- DSA - B Trees
- DSA - B+ Trees
- DSA - Splay Trees
- DSA - Spanning Tree
- DSA - Tries
- DSA - Heap
- Recursion
- DSA - Recursion Basics
- DSA - Tower of Hanoi
- DSA - Fibonacci Series
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Flood fill Algorithm
One matrix is given; the matrix is representing the one screen. Each element (i, j) of the screen is denoted as a pixel, the color of that pixel is marked with different numbers. In this algorithm, the pixels will be filled with new color when it is already in selected previous color. If the previous color is not the previous color, that pixel will not be filled. After filling a pixel, it will check for its up, down, left and right pixels to do the same.
The idea is really simple, first, we check whether the selected position is colored with the previous color or not, of not, the algorithm will not work. Otherwise, it will fill that pixel with new color and recur for its four neighbors.
Input and Output
Input: The screen matrix: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 2 2 2 2 0 1 0 1 1 1 2 2 0 1 0 1 1 1 2 2 2 2 0 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 Output: Screen matrix after flood fill 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 3 3 3 3 0 1 0 1 1 1 3 3 0 1 0 1 1 1 3 3 3 3 0 1 1 1 1 1 3 1 1 1 1 1 1 1 3 3 1
Algorithm
fillScreen(x, y, prevColor, newColor)
Input: The (x,y) coordinate to start, previous color, and new color.
Output − Screen after changing the color from previous to new, if possible.
Begin if (x, y) not in the screen range, then return if color of (x, y) ≠ prevColor, then return screen[x, y] := newColor fillScreen(x+1, y, prevColor, newColor) fillScreen(x-1, y, prevColor, newColor) fillScreen(x, y+1, prevColor, newColor) fillScreen(x, y-1, prevColor, newColor) End
Example
#include<iostream> #define M 8 #define N 8 using namespace std; int screen[M][N] = { //the screen dimention and colors {1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 0}, {1, 0, 0, 1, 1, 0, 1, 1}, {1, 2, 2, 2, 2, 0, 1, 0}, {1, 1, 1, 2, 2, 0, 1, 0}, {1, 1, 1, 2, 2, 2, 2, 0}, {1, 1, 1, 1, 1, 2, 1, 1}, {1, 1, 1, 1, 1, 2, 2, 1} }; void fillScreen(int x, int y, int prevColor, int newColor) { //replace previous color of (x,y), with new color if (x < 0 || x >= M || y < 0 || y >= N) //when point exceeds the screen return; if (screen[x][y] != prevColor) //if the point(x,y) are not containing prevColor, do nothing return; screen[x][y] = newColor; //update the color fillScreen(x+1, y, prevColor, newColor); //for the right of (x,y) fillScreen(x-1, y, prevColor, newColor); //for the left of (x,y) fillScreen(x, y+1, prevColor, newColor); //for the top of (x,y) fillScreen(x, y-1, prevColor, newColor); //for the bottom of (x,y) } void floodFill(int x, int y, int newColor) { int prevColor = screen[x][y]; //take the color before replacing with new color fillScreen(x, y, prevColor, newColor); } int main() { int x = 4, y = 4, newColor = 3; cout << "Previous screen: "<< endl; for (int i=0; i<M; i++) { for (int j=0; j<N; j++) cout << screen[i][j] << " "; cout << endl; } cout << endl; floodFill(x, y, newColor); //start from (4, 4), with new color 3 cout << "Updated screen: "<< endl; for (int i=0; i<M; i++) { for (int j=0; j<N; j++) cout << screen[i][j] << " "; cout << endl; } }
Output
Previous screen 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 2 2 2 2 0 1 0 1 1 1 2 2 0 1 0 1 1 1 2 2 2 2 0 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 Updated screen: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 3 3 3 3 0 1 0 1 1 1 3 3 0 1 0 1 1 1 3 3 3 3 0 1 1 1 1 1 3 1 1 1 1 1 1 1 3 3 1
- Related Articles
- Flood fill algorithm using C graphics
- Difference Between Flood-fill and Boundary-fill Algorithm
- Flood fill Algorithm – how to implement fill() in paint in C++
- Flood fill to specific color in PHP using imagefilltoborder() (GD) function.
- What is a Ping Flood Attack or ICMP Flood Attack?
- What is an IP Flood?
- What are flood routing and random routing?
- How do forests help in flood prevention?
- What is Flood Lighting? – Definition, Purpose, Calculation and Applications
- What role does forest play in the prevention of flood?
- Explain why, big boulders can be moved easily by flood.
- kasai’s Algorithm
- Manacher’s Algorithm
- Z Algorithm
- Fleury’s Algorithm
