In MS-Paint, when we take the brush to a pixel and click, the color of the region of that pixel is replaced with a new selected color. Following is the problem statement to do this task.
Given a 2D screen, location of a pixel in the screen and a color, replace color of the given pixel and all adjacent same colored pixels with the given color.
Example:
Input:
screen[M][N] = {{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},
};
x = 4, y = 4, newColor = 3
The values in the given 2D screen
indicate colors of the pixels.
x and y are coordinates of the brush,
newColor is the color that
should replace the previous color on
screen[x][y] and all surrounding
pixels with same color.
Output:
Screen should be changed to following.
screen[M][N] = {{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},
};
This question can be solved using either Recursion or BFS. Both the solutions are discussed below
Method 1 (Using Recursion): The idea is simple, we first replace the color of the current pixel, then recur for 4 surrounding points. The following is a detailed algorithm.
// A recursive function to replace
// previous color 'prevC' at '(x, y)'
// and all surrounding pixels of (x, y)
// with new color 'newC' and
floodFill(screen[M][N], x, y, prevC, newC)
1) If x or y is outside the screen, then return.
2) If color of screen[x][y] is not same as prevC, then return
3) Recur for north, south, east and west.
floodFillUtil(screen, x+1, y, prevC, newC);
floodFillUtil(screen, x-1, y, prevC, newC);
floodFillUtil(screen, x, y+1, prevC, newC);
floodFillUtil(screen, x, y-1, prevC, newC);
The following is the implementation of the above algorithm.
C++
#include<iostream>
using namespace std;
#define M 8
#define N 8
void floodFillUtil(int screen[][N], int x, int y, int prevC, int newC)
{
if (x < 0 || x >= M || y < 0 || y >= N)
return;
if (screen[x][y] != prevC)
return;
if (screen[x][y] == newC)
return;
screen[x][y] = newC;
floodFillUtil(screen, x+1, y, prevC, newC);
floodFillUtil(screen, x-1, y, prevC, newC);
floodFillUtil(screen, x, y+1, prevC, newC);
floodFillUtil(screen, x, y-1, prevC, newC);
}
void floodFill(int screen[][N], int x, int y, int newC)
{
int prevC = screen[x][y];
if(prevC==newC) return;
floodFillUtil(screen, x, y, prevC, newC);
}
int main()
{
int screen[M][N] = {{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},
};
int x = 4, y = 4, newC = 3;
floodFill(screen, x, y, newC);
cout << "Updated screen after call to floodFill: \n";
for (int i=0; i<M; i++)
{
for (int j=0; j<N; j++)
cout << screen[i][j] << " ";
cout << endl;
}
}
|
Java
import java.io.*;
class GFG
{
static int M = 8;
static int N = 8;
static void floodFillUtil(int screen[][], int x, int y,
int prevC, int newC)
{
if (x < 0 || x >= M || y < 0 || y >= N)
return;
if (screen[x][y] != prevC)
return;
screen[x][y] = newC;
floodFillUtil(screen, x+1, y, prevC, newC);
floodFillUtil(screen, x-1, y, prevC, newC);
floodFillUtil(screen, x, y+1, prevC, newC);
floodFillUtil(screen, x, y-1, prevC, newC);
}
static void floodFill(int screen[][], int x, int y, int newC)
{
int prevC = screen[x][y];
if(prevC==newC) return;
floodFillUtil(screen, x, y, prevC, newC);
}
public static void main(String[] args)
{
int 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},
};
int x = 4, y = 4, newC = 3;
floodFill(screen, x, y, newC);
System.out.println("Updated screen after call to floodFill: ");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
System.out.print(screen[i][j] + " ");
System.out.println();
}
}
}
|
Python3
M = 8
N = 8
def floodFillUtil(screen, x, y, prevC, newC):
if (x < 0 or x >= M or y < 0 or
y >= N or screen[x][y] != prevC or
screen[x][y] == newC):
return
screen[x][y] = newC
floodFillUtil(screen, x + 1, y, prevC, newC)
floodFillUtil(screen, x - 1, y, prevC, newC)
floodFillUtil(screen, x, y + 1, prevC, newC)
floodFillUtil(screen, x, y - 1, prevC, newC)
def floodFill(screen, x, y, newC):
prevC = screen[x][y]
if(prevC==newC):
return
floodFillUtil(screen, x, y, prevC, newC)
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]]
x = 4
y = 4
newC = 3
floodFill(screen, x, y, newC)
print ("Updated screen after call to floodFill:")
for i in range(M):
for j in range(N):
print(screen[i][j], end = ' ')
print()
|
C#
using System;
class GFG
{
static int M = 8;
static int N = 8;
static void floodFillUtil(int [,]screen,
int x, int y,
int prevC, int newC)
{
if (x < 0 || x >= M ||
y < 0 || y >= N)
return;
if (screen[x, y] != prevC)
return;
screen[x, y] = newC;
floodFillUtil(screen, x + 1, y, prevC, newC);
floodFillUtil(screen, x - 1, y, prevC, newC);
floodFillUtil(screen, x, y + 1, prevC, newC);
floodFillUtil(screen, x, y - 1, prevC, newC);
}
static void floodFill(int [,]screen, int x,
int y, int newC)
{
int prevC = screen[x, y];
if(prevC == newC)
return;
floodFillUtil(screen, x, y, prevC, newC);
}
public static void Main(String[] args)
{
int [,]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}};
int x = 4, y = 4, newC = 3;
floodFill(screen, x, y, newC);
Console.WriteLine("Updated screen after" +
"call to floodFill: ");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
Console.Write(screen[i, j] + " ");
Console.WriteLine();
}
}
}
|
Javascript
<script>
var M = 8;
var N = 8;
function floodFillUtil(screen, x, y, prevC, newC)
{
if (x < 0 || x >= M || y < 0 || y >= N) return;
if (screen[x][y] != prevC) return;
screen[x][y] = newC;
floodFillUtil(screen, x + 1, y, prevC, newC);
floodFillUtil(screen, x - 1, y, prevC, newC);
floodFillUtil(screen, x, y + 1, prevC, newC);
floodFillUtil(screen, x, y - 1, prevC, newC);
}
function floodFill(screen, x, y, newC) {
var prevC = screen[x][y];
if (prevC == newC) return;
floodFillUtil(screen, x, y, prevC, newC);
}
var 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],
];
var x = 4,
y = 4,
newC = 3;
floodFill(screen, x, y, newC);
document.write("Updated screen after" + "call to floodFill: <br>");
for (var i = 0; i < M; i++) {
for (var j = 0; j < N; j++) document.write(screen[i][j] + " ");
document.write("<br>");
}
</script>
|
Output
Updated screen after call to floodFill:
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
Time complexity: O(M x N).
Auxiliary Space: O(M x N), as implicit stack is created due to recursion
Method 2 (Using the BFS approach):
Algorithm for BFS based approach :
- Create a queue of pairs.
- Insert an initial index given in the queue.
- Mark initial index as visited in vis[][] matrix.
- Until the queue is not empty repeat step 4.1 to 4.6
- Take the front element from the queue
- Pop from the queue
- Store current value/color at coordinate taken out from queue (precolor)
- Update the value/color of the current index which is taken out from the queue
- Check for all 4 direction i.e (x+1,y),(x-1,y),(x,y+1),(x,y-1) is valid or not and if valid then check that value at that coordinate should be equal to precolor and value of that coordinate in vis[][] is 0.
- If all the above condition is true push the corresponding coordinate in queue and mark as 1 in vis[][]
- Print the matrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int validCoord(int x, int y, int n, int m)
{
if (x < 0 || y < 0) {
return 0;
}
if (x >= n || y >= m) {
return 0;
}
return 1;
}
void bfs(int n, int m, int data[][8],
int x, int y, int color)
{
int vis[101][101];
memset(vis, 0, sizeof(vis));
queue<pair<int, int> > obj;
obj.push({ x, y });
vis[x][y] = 1;
while (obj.empty() != 1)
{
pair<int, int> coord = obj.front();
int x = coord.first;
int y = coord.second;
int preColor = data[x][y];
data[x][y] = color;
obj.pop();
if (validCoord(x + 1, y, n, m)
&& vis[x + 1][y] == 0
&& data[x + 1][y] == preColor)
{
obj.push({ x + 1, y });
vis[x + 1][y] = 1;
}
if (validCoord(x - 1, y, n, m)
&& vis[x - 1][y] == 0
&& data[x - 1][y] == preColor)
{
obj.push({ x - 1, y });
vis[x - 1][y] = 1;
}
if (validCoord(x, y + 1, n, m)
&& vis[x][y + 1] == 0
&& data[x][y + 1] == preColor)
{
obj.push({ x, y + 1 });
vis[x][y + 1] = 1;
}
if (validCoord(x, y - 1, n, m)
&& vis[x][y - 1] == 0
&& data[x][y - 1] == preColor)
{
obj.push({ x, y - 1 });
vis[x][y - 1] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
int main()
{
int n, m, x, y, color;
n = 8;
m = 8;
int data[8][8] = {
{ 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 },
};
x = 4, y = 4, color = 3;
bfs(n, m, data, x, y, color);
return 0;
}
|
Java
import java.io.*;
class Pair implements Comparable<Pair> {
int first;
int second;
public Pair(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair o) {
return second - o.second;
}
}
class GFG {
public static int validCoord(int x, int y, int n, int m)
{
if (x < 0 || y < 0) {
return 0;
}
if (x >= n || y >= m) {
return 0;
}
return 1;
}
public static void bfs(int n, int m, int data[][],int x, int y, int color)
{
int vis[][]=new int[101][101];
for(int i=0;i<=100;i++){
for(int j=0;j<=100;j++){
vis[i][j]=0;
}
}
Queue<Pair> obj = new LinkedList<>();
Pair pq=new Pair(x,y);
obj.add(pq);
vis[x][y] = 1;
while (!obj.isEmpty())
{
Pair coord = obj.peek();
int x1 = coord.first;
int y1 = coord.second;
int preColor = data[x1][y1];
data[x1][y1] = color;
obj.remove();
if ((validCoord(x1 + 1, y1, n, m)==1) && vis[x1 + 1][y1] == 0 && data[x1 + 1][y1] == preColor)
{
Pair p=new Pair(x1 +1, y1);
obj.add(p);
vis[x1 + 1][y1] = 1;
}
if ((validCoord(x1 - 1, y1, n, m)==1) && vis[x1 - 1][y1] == 0 && data[x1 - 1][y1] == preColor)
{
Pair p=new Pair(x1-1,y1);
obj.add(p);
vis[x1- 1][y1] = 1;
}
if ((validCoord(x1, y1 + 1, n, m)==1) && vis[x1][y1 + 1] == 0 && data[x1][y1 + 1] == preColor)
{
Pair p=new Pair(x1,y1 +1);
obj.add(p);
vis[x1][y1 + 1] = 1;
}
if ((validCoord(x1, y1 - 1, n, m)==1) && vis[x1][y1 - 1] == 0 && data[x1][y1 - 1] == preColor)
{
Pair p=new Pair(x1,y1 -1);
obj.add(p);
vis[x1][y1 - 1] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
System.out.print(data[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
public static void main (String[] args) {
int nn, mm, xx, yy, colorr;
nn = 8;
mm = 8;
int data[][] = {{ 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 },};
xx = 4; yy = 4; colorr = 3;
bfs(nn, mm, data, xx, yy, colorr);
}
}
|
Python3
def validCoord(x, y, n, m):
if x < 0 or y < 0:
return 0
if x >= n or y >= m:
return 0
return 1
def bfs(n, m, data, X, Y, color):
vis = [[0 for i in range(101)] for j in range(101)]
obj = []
obj.append([X, Y])
vis[X][Y] = 1
while len(obj) > 0:
coord = obj[0]
x = coord[0]
y = coord[1]
preColor = data[x][y]
data[x][y] = color
obj.pop(0)
if validCoord(x + 1, y, n, m) == 1 and vis[x + 1][y] == 0 and data[x + 1][y] == preColor:
obj.append([x + 1, y])
vis[x + 1][y] = 1
if validCoord(x - 1, y, n, m) == 1 and vis[x - 1][y] == 0 and data[x - 1][y] == preColor:
obj.append([x - 1, y])
vis[x - 1][y] = 1
if validCoord(x, y + 1, n, m) == 1 and vis[x][y + 1] == 0 and data[x][y + 1] == preColor:
obj.append([x, y + 1])
vis[x][y + 1] = 1
if validCoord(x, y - 1, n, m) == 1 and vis[x][y - 1] == 0 and data[x][y - 1] == preColor:
obj.append([x, y - 1])
vis[x][y - 1] = 1
for i in range(n):
for j in range(m):
print(data[i][j], end = " ")
print()
print()
n = 8
m = 8
data = [
[ 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 ],
]
x, y, color = 4, 4, 3
bfs(n, m, data, x, y, color)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int validCoord(int x, int y, int n, int m)
{
if (x < 0 || y < 0) {
return 0;
}
if (x >= n || y >= m) {
return 0;
}
return 1;
}
static void bfs(int n, int m, int[,] data, int X, int Y, int color)
{
int[,] vis = new int[101,101];
List<Tuple<int, int>> obj = new List<Tuple<int, int>>();
obj.Add(new Tuple<int,int>( X, Y ));
vis[X,Y] = 1;
while (obj.Count > 0)
{
Tuple<int, int> coord = obj[0];
int x = coord.Item1;
int y = coord.Item2;
int preColor = data[x,y];
data[x,y] = color;
obj.RemoveAt(0);
if (validCoord(x + 1, y, n, m) == 1
&& vis[x + 1,y] == 0
&& data[x + 1,y] == preColor)
{
obj.Add(new Tuple<int,int>(x + 1, y));
vis[x + 1,y] = 1;
}
if (validCoord(x - 1, y, n, m) == 1
&& vis[x - 1,y] == 0
&& data[x - 1,y] == preColor)
{
obj.Add(new Tuple<int,int>(x - 1, y));
vis[x - 1,y] = 1;
}
if (validCoord(x, y + 1, n, m) == 1
&& vis[x,y + 1] == 0
&& data[x,y + 1] == preColor)
{
obj.Add(new Tuple<int,int>(x, y + 1));
vis[x,y + 1] = 1;
}
if (validCoord(x, y - 1, n, m) == 1
&& vis[x,y - 1] == 0
&& data[x,y - 1] == preColor)
{
obj.Add(new Tuple<int,int>(x, y - 1));
vis[x,y - 1] = 1;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
Console.Write(data[i,j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
static void Main() {
int n, m, x, y, color;
n = 8;
m = 8;
int[,] data = {
{ 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 },
};
x = 4;
y = 4;
color = 3;
bfs(n, m, data, x, y, color);
}
}
|
Javascript
<script>
function validCoord(x, y, n, m)
{
if (x < 0 || y < 0) {
return 0;
}
if (x >= n || y >= m) {
return 0;
}
return 1;
}
function bfs(n, m, data, X, Y, color)
{
let vis = new Array(101);
for(let i = 0; i < 101; i++)
{
vis[i] = new Array(101);
for(let j = 0; j < 101; j++)
{
vis[i][j] = 0;
}
}
let obj = [];
obj.push([X, Y]);
vis[X][Y] = 1;
while (obj.length > 0)
{
let coord = obj[0];
let x = coord[0];
let y = coord[1];
let preColor = data[x][y];
data[x][y] = color;
obj.shift();
if (validCoord(x + 1, y, n, m) == 1
&& vis[x + 1][y] == 0
&& data[x + 1][y] == preColor)
{
obj.push([x + 1, y]);
vis[x + 1][y] = 1;
}
if (validCoord(x - 1, y, n, m) == 1
&& vis[x - 1][y] == 0
&& data[x - 1][y] == preColor)
{
obj.push([x - 1, y]);
vis[x - 1][y] = 1;
}
if (validCoord(x, y + 1, n, m) == 1
&& vis[x][y + 1] == 0
&& data[x][y + 1] == preColor)
{
obj.push([x, y + 1]);
vis[x][y + 1] = 1;
}
if (validCoord(x, y - 1, n, m) == 1
&& vis[x][y - 1] == 0
&& data[x][y - 1] == preColor)
{
obj.push([x, y - 1]);
vis[x][y - 1] = 1;
}
}
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
document.write(data[i][j] + " ");
}
document.write("</br>");
}
document.write("</br>");
}
let n, m, x, y, color;
n = 8;
m = 8;
let data = [
[ 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 ],
];
x = 4, y = 4, color = 3;
bfs(n, m, data, x, y, color);
</script>
|
Output
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
Time complexity: O(M x N).
Auxiliary Space: O(M x N),
Last Updated :
12 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...