Flood fill Algorithm
The Flood fill algorithm is a simple recursive algorithm used to fill a contiguous area of pixels in an image or a 2D array with a specific color or pattern.
The algorithm starts at a given pixel and checks its color. It then checks the neighboring pixels and, if they have the same color, changes their color and recursively calls the Flood fill algorithm on those pixels. This process continues until all pixels that are reachable from the starting pixel and have the same color have been colored.
Flood fill algorithm can be used in a variety of applications such as image processing, computer graphics, and game development. It is commonly used in paint applications to fill areas of the canvas with a particular color.
Here is the pseudocode for the Flood fill algorithm:
floodFill(x, y, oldColor, newColor):
if pixel (x, y) is not oldColor:
return
set pixel (x, y) to newColor
floodFill(x+1, y, oldColor, newColor)
floodFill(x-1, y, oldColor, newColor)
floodFill(x, y+1, oldColor, newColor)
floodFill(x, y-1, oldColor, newColor)
In this algorithm, (x, y)
is the starting pixel, oldColor
is the color to be replaced, and newColor
is the new color to be used. The algorithm sets the starting pixel to the new color and then recursively calls itself on the neighboring pixels. This continues until all pixels that are reachable from the starting pixel and have the same color as the starting pixel have been colored with the new color.
Here given code implementation process.
/*
C program
Flood fill Algorithm
*/
#include <stdio.h>
#define R 7
#define C 6
//Displaying of the matrix elements
void printMatrix(int matrix[R][C])
{
int i=0;
int j=0;
for (i = 0; i < R; ++i)
{
for (j = 0; j < C; ++j)
{
printf(" %d", matrix[i][j]);
}
printf("\n");
}
printf("\n");
}
//This is Recursively change the old color to new color of a given grid location
void floodFill(int x, int y,int color,int oldColor, int matrix[R][C])
{
if(x>=R || y >= C || y < 0 || x < 0)
{
// When size are not valid to given matrix
return;
}
if(matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
floodFill(x+1,y,color,oldColor,matrix);
floodFill(x,y+1,color,oldColor,matrix);
floodFill(x-1,y,color,oldColor,matrix);
floodFill(x,y-1,color,oldColor,matrix);
}
}
// Handles the request to perform fill color operation
void fillColor(int x, int y,int color, int matrix[R][C])
{
if(x>=R || y >= C || y < 0 || x < 0)
{
printf("\n Invalid Position (%d , %d)",x,y);
return;
}
printf("\n Before fill color %d in Start (%d , %d) Location\n",color,x,y);
printMatrix(matrix);
floodFill(x,y,color,matrix[x][y],matrix);
printf("\n After fill color %d in Start (%d , %d) Location \n",color,x,y);
printMatrix(matrix);
}
int main()
{
// Define matrix of an integer elements
int matrix[R][C] =
{
{9, 9, 9 , 9, 9, 9 },
{9, 1, 9 , 2, 6, 9 },
{1, 1, 9 , 2, 9, 9 },
{1, 1, 1 , 2, 9, 9 },
{1, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 2, 2 }
};
// Location
int x = 1;
int y = 2;
int color = 4;
fillColor(x,y,color,matrix);
return 0;
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
/*
Java Program
Flood fill Algorithm
*/
public class MyMatrix
{
//Displaying of the matrix elements
public void printMatrix(int[][] matrix, int rows, int cols)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
System.out.print(" " + matrix[i][j]);
}
System.out.print("\n");
}
System.out.print("\n");
}
//This is Recursively change the old color to new color of a given grid location
public void floodFill(int x, int y, int color, int oldColor, int[][] matrix, int rows, int cols)
{
if (x >= rows || y >= cols || y < 0 || x < 0)
{
// When size are not valid to given matrix
return;
}
if (matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
floodFill(x + 1, y, color, oldColor, matrix, rows,cols);
floodFill(x, y + 1, color, oldColor, matrix, rows,cols);
floodFill(x - 1, y, color, oldColor, matrix, rows,cols);
floodFill(x, y - 1, color, oldColor, matrix, rows,cols);
}
}
// Handles the request to perform fill color operation
public void fillColor(int x, int y, int color, int[][] matrix)
{
// Get the size
int rows = matrix.length;
int cols = matrix[0].length;
if (y < 0 || x < 0 || x >= rows || y >= cols)
{
System.out.print("\n Invalid Position (" + x + " , " + y + ")");
return;
}
System.out.print("\n Before fill color " + color + " in Start (" + x + " , " + y + ") Location\n");
printMatrix(matrix, rows, cols);
floodFill(x, y, color, matrix[x][y], matrix, rows, cols);
System.out.print("\n After fill color " + color + " in Start (" + x + " , " + y + ") Location \n");
printMatrix(matrix, rows, cols);
}
public static void main(String[] args)
{
MyMatrix obj = new MyMatrix();
int [][]matrix =
{
{9, 9, 9 , 9, 9, 9 },
{9, 1, 9 , 2, 6, 9 },
{1, 1, 9 , 2, 9, 9 },
{1, 1, 1 , 2, 9, 9 },
{1, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 2, 2 }
};
// Location
int x = 1;
int y = 2;
// new color
int color = 4;
obj.fillColor(x, y, color, matrix);
}
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
// Include header file
#include <iostream>
#define R 7
#define C 6
using namespace std;
/*
C++ Program
Flood fill Algorithm
*/
class MyMatrix
{
public:
// Displaying of the matrix elements
void printMatrix(int matrix[R][C])
{
int i = 0;
int j = 0;
for (i = 0; i < R; ++i)
{
for (j = 0; j < C; ++j)
{
cout << " " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
// This is Recursively change the old color to new color of a given grid location
void floodFill(int x, int y, int color, int oldColor, int matrix[R][C])
{
// When size are not valid to given matrix
if (x >= R || y >= C || y < 0 || x < 0)
{
return;
}
if (matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
this->floodFill(x + 1, y, color, oldColor, matrix);
this->floodFill(x, y + 1, color, oldColor, matrix);
this->floodFill(x - 1, y, color, oldColor, matrix);
this->floodFill(x, y - 1, color, oldColor, matrix);
}
}
// Handles the request to perform fill color operation
void fillColor(int x, int y, int color, int matrix[R][C])
{
if (y < 0 || x < 0 || x >= R || y >= C)
{
cout << "\n Invalid Position (" << x << " , " << y << ")";
return;
}
cout << "\n Before fill color " << color << " in Start (" << x << " , " << y << ") Location\n";
this->printMatrix(matrix);
this->floodFill(x, y, color, matrix[x][y], matrix);
cout << "\n After fill color " << color << " in Start (" << x << " , " << y << ") Location \n";
this->printMatrix(matrix);
}
};
int main()
{
MyMatrix obj = MyMatrix();
int matrix[R][C] =
{
{9, 9, 9 , 9, 9, 9 },
{9, 1, 9 , 2, 6, 9 },
{1, 1, 9 , 2, 9, 9 },
{1, 1, 1 , 2, 9, 9 },
{1, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 2, 2 }
};
// Location
int x = 1;
int y = 2;
// new color
int color = 4;
obj.fillColor(x, y, color, matrix);
return 0;
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
// Include namespace system
using System;
/*
C# Program
Flood fill Algorithm
*/
public class MyMatrix
{
// Displaying of the matrix elements
public void printMatrix(int[,] matrix, int rows, int cols)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
// This is Recursively change the old color to new color of a given grid location
public void floodFill(int x, int y, int color, int oldColor, int[,] matrix, int rows, int cols)
{
// When size are not valid to given matrix
if (x >= rows || y >= cols || y < 0 || x < 0)
{
return;
}
if (matrix[x,y] == oldColor)
{
// Change existing color
matrix[x,y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
floodFill(x + 1, y, color, oldColor, matrix, rows, cols);
floodFill(x, y + 1, color, oldColor, matrix, rows, cols);
floodFill(x - 1, y, color, oldColor, matrix, rows, cols);
floodFill(x, y - 1, color, oldColor, matrix, rows, cols);
}
}
// Handles the request to perform fill color operation
public void fillColor(int x, int y, int color, int[,] matrix)
{
// Get the size
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
if (y < 0 || x < 0 || x >= rows || y >= cols)
{
Console.Write("\n Invalid Position (" + x + " , " + y + ")");
return;
}
Console.Write("\n Before fill color " + color + " in Start (" + x + " , " + y + ") Location\n");
printMatrix(matrix, rows, cols);
floodFill(x, y, color, matrix[x,y], matrix, rows, cols);
Console.Write("\n After fill color " + color + " in Start (" + x + " , " + y + ") Location \n");
printMatrix(matrix, rows, cols);
}
public static void Main(String[] args)
{
MyMatrix obj = new MyMatrix();
int[,] matrix =
{
{9, 9, 9 , 9, 9, 9 },
{9, 1, 9 , 2, 6, 9 },
{1, 1, 9 , 2, 9, 9 },
{1, 1, 1 , 2, 9, 9 },
{1, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 1, 1 },
{2, 2, 1 , 2, 2, 2 }
};
// Location
int x = 1;
int y = 2;
// new color
int color = 4;
obj.fillColor(x, y, color, matrix);
}
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
<?php
/*
Php Program
Flood fill Algorithm
*/
class MyMatrix
{
// Displaying of the matrix elements
public function printMatrix( & $matrix, $rows, $cols)
{
$i = 0;
$j = 0;
for ($i = 0; $i < $rows; ++$i)
{
for ($j = 0; $j < $cols; ++$j)
{
echo " ". $matrix[$i][$j];
}
echo "\n";
}
echo "\n";
}
// This is Recursively change the old color to new color of a given grid location
public function floodFill($x, $y, $color, $oldColor, & $matrix, $rows, $cols)
{
// When size are not valid to given matrix
if ($x >= $rows || $y >= $cols || $y < 0 || $x < 0)
{
return;
}
if ($matrix[$x][$y] == $oldColor)
{
// Change existing color
$matrix[$x][$y] = $color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
$this->floodFill($x + 1, $y, $color, $oldColor, $matrix, $rows, $cols);
$this->floodFill($x, $y + 1, $color, $oldColor, $matrix, $rows, $cols);
$this->floodFill($x - 1, $y, $color, $oldColor, $matrix, $rows, $cols);
$this->floodFill($x, $y - 1, $color, $oldColor, $matrix, $rows, $cols);
}
}
// Handles the request to perform fill color operation
public function fillColor($x, $y, $color, & $matrix)
{
// Get the size
$rows = count($matrix);
$cols = count($matrix[0]);
if ($y < 0 || $x < 0 || $x >= $rows || $y >= $cols)
{
echo "\n Invalid Position (". $x ." , ". $y .")";
return;
}
echo "\n Before fill color ". $color ." in Start (". $x ." , ". $y .") Location\n";
$this->printMatrix($matrix, $rows, $cols);
$this->floodFill($x, $y, $color, $matrix[$x][$y], $matrix, $rows, $cols);
echo "\n After fill color ". $color ." in Start (". $x ." , ". $y .") Location \n";
$this->printMatrix($matrix, $rows, $cols);
}
}
function main()
{
$obj = new MyMatrix();
$matrix = array(
array(9, 9, 9, 9, 9, 9),
array(9, 1, 9, 2, 6, 9),
array(1, 1, 9, 2, 9, 9),
array(1, 1, 1, 2, 9, 9),
array(1, 2, 1, 2, 1, 1),
array(2, 2, 1, 2, 1, 1),
array(2, 2, 1, 2, 2, 2)
);
// Location
$x = 1;
$y = 2;
// new color
$color = 4;
$obj->fillColor($x, $y, $color, $matrix);
}
main();
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
/*
Node Js Program
Flood fill Algorithm
*/
class MyMatrix
{
// Displaying of the matrix elements
printMatrix(matrix, rows, cols)
{
var i = 0;
var j = 0;
for (i = 0; i < rows; ++i)
{
for (j = 0; j < cols; ++j)
{
process.stdout.write(" " + matrix[i][j]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
// This is Recursively change the old color to new color of a given grid location
floodFill(x, y, color, oldColor, matrix, rows, cols)
{
// When size are not valid to given matrix
if (x >= rows || y >= cols || y < 0 || x < 0)
{
return;
}
if (matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
this.floodFill(x + 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y + 1, color, oldColor, matrix, rows, cols);
this.floodFill(x - 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y - 1, color, oldColor, matrix, rows, cols);
}
}
// Handles the request to perform fill color operation
fillColor(x, y, color, matrix)
{
// Get the size
var rows = matrix.length;
var cols = matrix[0].length;
if (y < 0 || x < 0 || x >= rows || y >= cols)
{
process.stdout.write("\n Invalid Position (" + x + " , " + y + ")");
return;
}
process.stdout.write("\n Before fill color " + color + " in Start (" + x + " , " + y + ") Location\n");
this.printMatrix(matrix, rows, cols);
this.floodFill(x, y, color, matrix[x][y], matrix, rows, cols);
process.stdout.write("\n After fill color " + color + " in Start (" + x + " , " + y + ") Location \n");
this.printMatrix(matrix, rows, cols);
}
}
function main()
{
var obj = new MyMatrix();
var matrix = [
[9, 9, 9, 9, 9, 9] ,
[9, 1, 9, 2, 6, 9] ,
[1, 1, 9, 2, 9, 9] ,
[1, 1, 1, 2, 9, 9] ,
[1, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 2, 2]
];
// Location
var x = 1;
var y = 2;
// new color
var color = 4;
obj.fillColor(x, y, color, matrix);
}
main();
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
# Python 3 Program
# Flood fill Algorithm
class MyMatrix :
# Displaying of the matrix elements
def printMatrix(self, matrix, rows, cols) :
i = 0
j = 0
while (i < rows) :
j = 0
while (j < cols) :
print(" ", matrix[i][j], end = "")
j += 1
print(end = "\n")
i += 1
print(end = "\n")
# This is Recursively change the old color to new color of a given grid location
def floodFill(self, x, y, color, oldColor, matrix, rows, cols) :
# When size are not valid to given matrix
if (x >= rows or y >= cols or y < 0 or x < 0) :
return
if (matrix[x][y] == oldColor) :
# Change existing color
matrix[x][y] = color
# Visit to adjacent elements
# Recursively visiting of matrix elements
self.floodFill(x + 1, y, color, oldColor, matrix, rows, cols)
self.floodFill(x, y + 1, color, oldColor, matrix, rows, cols)
self.floodFill(x - 1, y, color, oldColor, matrix, rows, cols)
self.floodFill(x, y - 1, color, oldColor, matrix, rows, cols)
# Handles the request to perform fill color operation
def fillColor(self, x, y, color, matrix) :
# Get the size
rows = len(matrix)
cols = len(matrix[0])
if (y < 0 or x < 0 or x >= rows or y >= cols) :
print("\n Invalid Position (", x ," , ", y ,")", end = "")
return
print("\n Before fill color ", color ," in Start (", x ," , ", y ,") Location")
self.printMatrix(matrix, rows, cols)
self.floodFill(x, y, color, matrix[x][y], matrix, rows, cols)
print("\n After fill color ", color ," in Start (", x ," , ", y ,") Location ")
self.printMatrix(matrix, rows, cols)
def main() :
obj = MyMatrix()
matrix = [
[9, 9, 9, 9, 9, 9] ,
[9, 1, 9, 2, 6, 9] ,
[1, 1, 9, 2, 9, 9] ,
[1, 1, 1, 2, 9, 9] ,
[1, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 2, 2]
]
# Location
x = 1
y = 2
# new color
color = 4
obj.fillColor(x, y, color, matrix)
if __name__ == "__main__": main()
Output
Before fill color 4 in Start ( 1 , 2 ) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start ( 1 , 2 ) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
# Ruby Program
# Flood fill Algorithm
class MyMatrix
# Displaying of the matrix elements
def printMatrix(matrix, rows, cols)
i = 0
j = 0
while (i < rows)
j = 0
while (j < cols)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
# This is Recursively change the old color to new color of a given grid location
def floodFill(x, y, color, oldColor, matrix, rows, cols)
# When size are not valid to given matrix
if (x >= rows || y >= cols || y < 0 || x < 0)
return
end
if (matrix[x][y] == oldColor)
# Change existing color
matrix[x][y] = color
# Visit to adjacent elements
# Recursively visiting of matrix elements
self.floodFill(x + 1, y, color, oldColor, matrix, rows, cols)
self.floodFill(x, y + 1, color, oldColor, matrix, rows, cols)
self.floodFill(x - 1, y, color, oldColor, matrix, rows, cols)
self.floodFill(x, y - 1, color, oldColor, matrix, rows, cols)
end
end
# Handles the request to perform fill color operation
def fillColor(x, y, color, matrix)
# Get the size
rows = matrix.length
cols = matrix[0].length
if (y < 0 || x < 0 || x >= rows || y >= cols)
print("\n Invalid Position (", x ," , ", y ,")")
return
end
print("\n Before fill color ", color ," in Start (", x ," , ", y ,") Location\n")
self.printMatrix(matrix, rows, cols)
self.floodFill(x, y, color, matrix[x][y], matrix, rows, cols)
print("\n After fill color ", color ," in Start (", x ," , ", y ,") Location \n")
self.printMatrix(matrix, rows, cols)
end
end
def main()
obj = MyMatrix.new()
matrix = [
[9, 9, 9, 9, 9, 9] ,
[9, 1, 9, 2, 6, 9] ,
[1, 1, 9, 2, 9, 9] ,
[1, 1, 1, 2, 9, 9] ,
[1, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 2, 2]
]
# Location
x = 1
y = 2
# new color
color = 4
obj.fillColor(x, y, color, matrix)
end
main()
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
/*
Scala Program
Flood fill Algorithm
*/
class MyMatrix
{
// Displaying of the matrix elements
def printMatrix(matrix: Array[Array[Int]], rows: Int, cols: Int): Unit = {
var i: Int = 0;
var j: Int = 0;
while (i < rows)
{
j = 0;
while (j < cols)
{
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
// This is Recursively change the old color to new color of a given grid location
def floodFill(x: Int, y: Int, color: Int, oldColor: Int, matrix: Array[Array[Int]], rows: Int, cols: Int): Unit = {
// When size are not valid to given matrix
if (x >= rows || y >= cols || y < 0 || x < 0)
{
return;
}
if (matrix(x)(y) == oldColor)
{
// Change existing color
matrix(x)(y) = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
this.floodFill(x + 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y + 1, color, oldColor, matrix, rows, cols);
this.floodFill(x - 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y - 1, color, oldColor, matrix, rows, cols);
}
}
// Handles the request to perform fill color operation
def fillColor(x: Int, y: Int, color: Int, matrix: Array[Array[Int]]): Unit = {
// Get the size
var rows: Int = matrix.length;
var cols: Int = matrix(0).length;
if (y < 0 || x < 0 || x >= rows || y >= cols)
{
print("\n Invalid Position (" + x + " , " + y + ")");
return;
}
print("\n Before fill color " + color + " in Start (" + x + " , " + y + ") Location\n");
this.printMatrix(matrix, rows, cols);
this.floodFill(x, y, color, matrix(x)(y), matrix, rows, cols);
print("\n After fill color " + color + " in Start (" + x + " , " + y + ") Location \n");
this.printMatrix(matrix, rows, cols);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyMatrix = new MyMatrix();
var matrix: Array[Array[Int]] = Array(
Array(9, 9, 9, 9, 9, 9),
Array(9, 1, 9, 2, 6, 9),
Array(1, 1, 9, 2, 9, 9),
Array(1, 1, 1, 2, 9, 9),
Array(1, 2, 1, 2, 1, 1),
Array(2, 2, 1, 2, 1, 1),
Array(2, 2, 1, 2, 2, 2)
);
// Location
var x: Int = 1;
var y: Int = 2;
// new color
var color: Int = 4;
obj.fillColor(x, y, color, matrix);
}
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
/*
Swift 4 Program
Flood fill Algorithm
*/
class MyMatrix
{
// Displaying of the matrix elements
func printMatrix(_ matrix: [[Int]], _ rows: Int, _ cols: Int)
{
var i: Int = 0;
var j: Int = 0;
while (i < rows)
{
j = 0;
while (j < cols)
{
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print(terminator: "\n");
i += 1;
}
print(terminator: "\n");
}
// This is Recursively change the old color to new color of a given grid location
func floodFill(_ x: Int, _ y: Int, _ color: Int, _ oldColor: Int, _ matrix: inout[[Int]], _ rows: Int, _ cols: Int)
{
// When size are not valid to given matrix
if (x >= rows || y >= cols || y < 0 || x < 0)
{
return;
}
if (matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
self.floodFill(x + 1, y, color, oldColor, &matrix, rows, cols);
self.floodFill(x, y + 1, color, oldColor, &matrix, rows, cols);
self.floodFill(x - 1, y, color, oldColor, &matrix, rows, cols);
self.floodFill(x, y - 1, color, oldColor, &matrix, rows, cols);
}
}
// Handles the request to perform fill color operation
func fillColor(_ x: Int, _ y: Int, _ color: Int, _ matrix: inout[[Int]])
{
// Get the size
let rows: Int = matrix.count;
let cols: Int = matrix[0].count;
if (y < 0 || x < 0 || x >= rows || y >= cols)
{
print("\n Invalid Position (", x ," , ", y ,")", terminator: "");
return;
}
print("\n Before fill color ", color ," in Start (", x ," , ", y ,") Location");
self.printMatrix(matrix, rows, cols);
self.floodFill(x, y, color, matrix[x][y], &matrix, rows, cols);
print("\n After fill color ", color ," in Start (", x ," , ", y ,") Location ");
self.printMatrix(matrix, rows, cols);
}
}
func main()
{
let obj: MyMatrix = MyMatrix();
var matrix: [[Int]] = [
[9, 9, 9, 9, 9, 9] ,
[9, 1, 9, 2, 6, 9] ,
[1, 1, 9, 2, 9, 9] ,
[1, 1, 1, 2, 9, 9] ,
[1, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 1, 1] ,
[2, 2, 1, 2, 2, 2]
];
// Location
let x: Int = 1;
let y: Int = 2;
// new color
let color: Int = 4;
obj.fillColor(x, y, color, &matrix);
}
main();
Output
Before fill color 4 in Start ( 1 , 2 ) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start ( 1 , 2 ) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
/*
Kotlin Program
Flood fill Algorithm
*/
class MyMatrix
{
// Displaying of the matrix elements
fun printMatrix(matrix: Array<Array<Int>> , rows: Int, cols: Int): Unit
{
var i: Int = 0;
var j: Int = 0;
while (i<rows)
{
while (j<cols)
{
print(" " + matrix[i][j]);
j += 1;
}
print("\n");
i += 1;
j = 0;
}
print("\n");
}
// This is Recursively change the old color to new color of a given grid location
fun floodFill(x: Int, y: Int, color: Int, oldColor: Int, matrix: Array<Array<Int>> , rows: Int, cols: Int): Unit
{
// When size are not valid to given matrix
if (x>= rows || y>= cols || y<0 || x<0)
{
return;
}
if (matrix[x][y] == oldColor)
{
// Change existing color
matrix[x][y] = color;
// Visit to adjacent elements
// Recursively visiting of matrix elements
this.floodFill(x + 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y + 1, color, oldColor, matrix, rows, cols);
this.floodFill(x - 1, y, color, oldColor, matrix, rows, cols);
this.floodFill(x, y - 1, color, oldColor, matrix, rows, cols);
}
}
// Handles the request to perform fill color operation
fun fillColor(x: Int, y: Int, color: Int, matrix: Array<Array<Int>> ): Unit
{
// Get the size
var rows: Int = matrix.count();
var cols: Int = matrix[0].count();
if (y<0 || x<0 || x>= rows || y>= cols)
{
print("\n Invalid Position (" + x + " , " + y + ")");
return;
}
print("\n Before fill color " + color + " in Start (" + x + " , " + y + ") Location\n");
this.printMatrix(matrix, rows, cols);
this.floodFill(x, y, color, matrix[x][y], matrix, rows, cols);
print("\n After fill color " + color + " in Start (" + x + " , " + y + ") Location \n");
this.printMatrix(matrix, rows, cols);
}
}
fun main(args: Array<String>): Unit
{
var obj: MyMatrix = MyMatrix();
var matrix: Array<Array<Int>> = arrayOf(
arrayOf(9, 9, 9, 9, 9, 9),
arrayOf(9, 1, 9, 2, 6, 9),
arrayOf(1, 1, 9, 2, 9, 9),
arrayOf(1, 1, 1, 2, 9, 9),
arrayOf(1, 2, 1, 2, 1, 1),
arrayOf(2, 2, 1, 2, 1, 1),
arrayOf(2, 2, 1, 2, 2, 2)
);
// Location
var x: Int = 1;
var y: Int = 2;
// new color
var color: Int = 4;
obj.fillColor(x, y, color, matrix);
}
Output
Before fill color 4 in Start (1 , 2) Location
9 9 9 9 9 9
9 1 9 2 6 9
1 1 9 2 9 9
1 1 1 2 9 9
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
After fill color 4 in Start (1 , 2) Location
4 4 4 4 4 4
4 1 4 2 6 4
1 1 4 2 4 4
1 1 1 2 4 4
1 2 1 2 1 1
2 2 1 2 1 1
2 2 1 2 2 2
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment