You're developing a new digital art application called "PixelPerfect" that allows users to create pixel art. One of the essential tools in any digital art software is the paint bucket (or fill) tool, which fills a connected area with a new color.
When a user clicks on a pixel in the canvas, the paint bucket tool should change the color of that pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Your task is to implement the flood fill algorithm that powers this paint bucket tool. The algorithm should efficiently fill the connected region with the new color while ensuring that only pixels of the original color are changed.
Input: canvas = [
['R', 'R', 'R', 'R'],
['R', 'B', 'B', 'R'],
['R', 'B', 'B', 'R'],
['R', 'R', 'R', 'R']
]
startRow = 1, startCol = 1, newColor = 'G'
Output: [
['R', 'R', 'R', 'R'],
['R', 'G', 'G', 'R'],
['R', 'G', 'G', 'R'],
['R', 'R', 'R', 'R']
]
Explanation: Starting from position (1,1) with color 'B', we fill all connected 'B' pixels with 'G'.
Input: canvas = [
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y']
]
startRow = 0, startCol = 0, newColor = 'P'
Output: [
['P', 'P', 'P'],
['P', 'P', 'P'],
['P', 'P', 'P']
]
Explanation: Since all pixels are the same color and connected, the entire canvas is filled with the new color.
Input: canvas = [
['R', 'G', 'B'],
['G', 'R', 'G'],
['B', 'G', 'R']
]
startRow = 1, startCol = 1, newColor = 'Y'
Output: [
['R', 'G', 'B'],
['G', 'Y', 'G'],
['B', 'G', 'R']
]
Explanation: Starting from position (1,1) with color 'R', only that single pixel changes to 'Y' because none of its adjacent pixels have the same original color.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You're developing a new digital art application called "PixelPerfect" that allows users to create pixel art. One of the essential tools in any digital art software is the paint bucket (or fill) tool, which fills a connected area with a new color.
When a user clicks on a pixel in the canvas, the paint bucket tool should change the color of that pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Your task is to implement the flood fill algorithm that powers this paint bucket tool. The algorithm should efficiently fill the connected region with the new color while ensuring that only pixels of the original color are changed.
Starting from position (1,1) with color 'B', we fill all connected 'B' pixels with 'G'.
Since all pixels are the same color and connected, the entire canvas is filled with the new color.
Starting from position (1,1) with color 'R', only that single pixel changes to 'Y' because none of its adjacent pixels have the same original color.
This problem is a classic application of the Depth-First Search (DFS) pattern in recursion.
The recursive approach naturally handles the exploration of connected pixels.
We need to be careful not to revisit pixels we've already processed to avoid infinite recursion.
The algorithm can also be implemented iteratively using a queue (BFS) or stack (DFS), but the recursive approach is often more intuitive.
This problem has several practical applications:
Paint bucket tools in applications like Photoshop, GIMP, and MS Paint use flood fill algorithms.
Flood fill is used in image segmentation, object detection, and boundary extraction in computer vision.
Used in games for map generation, territory control visualization, and path finding.
You're developing a new digital art application called "PixelPerfect" that allows users to create pixel art. One of the essential tools in any digital art software is the paint bucket (or fill) tool, which fills a connected area with a new color.
When a user clicks on a pixel in the canvas, the paint bucket tool should change the color of that pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Your task is to implement the flood fill algorithm that powers this paint bucket tool. The algorithm should efficiently fill the connected region with the new color while ensuring that only pixels of the original color are changed.
Input: canvas = [
['R', 'R', 'R', 'R'],
['R', 'B', 'B', 'R'],
['R', 'B', 'B', 'R'],
['R', 'R', 'R', 'R']
]
startRow = 1, startCol = 1, newColor = 'G'
Output: [
['R', 'R', 'R', 'R'],
['R', 'G', 'G', 'R'],
['R', 'G', 'G', 'R'],
['R', 'R', 'R', 'R']
]
Explanation: Starting from position (1,1) with color 'B', we fill all connected 'B' pixels with 'G'.
Input: canvas = [
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y'],
['Y', 'Y', 'Y']
]
startRow = 0, startCol = 0, newColor = 'P'
Output: [
['P', 'P', 'P'],
['P', 'P', 'P'],
['P', 'P', 'P']
]
Explanation: Since all pixels are the same color and connected, the entire canvas is filled with the new color.
Input: canvas = [
['R', 'G', 'B'],
['G', 'R', 'G'],
['B', 'G', 'R']
]
startRow = 1, startCol = 1, newColor = 'Y'
Output: [
['R', 'G', 'B'],
['G', 'Y', 'G'],
['B', 'G', 'R']
]
Explanation: Starting from position (1,1) with color 'R', only that single pixel changes to 'Y' because none of its adjacent pixels have the same original color.
To solve this problem, we need to:
Apply string manipulation concepts to solve a real-world problem.
You're developing a new digital art application called "PixelPerfect" that allows users to create pixel art. One of the essential tools in any digital art software is the paint bucket (or fill) tool, which fills a connected area with a new color.
When a user clicks on a pixel in the canvas, the paint bucket tool should change the color of that pixel and all adjacent pixels of the same color, continuing until no more connected pixels of the original color remain.
Your task is to implement the flood fill algorithm that powers this paint bucket tool. The algorithm should efficiently fill the connected region with the new color while ensuring that only pixels of the original color are changed.
Starting from position (1,1) with color 'B', we fill all connected 'B' pixels with 'G'.
Since all pixels are the same color and connected, the entire canvas is filled with the new color.
Starting from position (1,1) with color 'R', only that single pixel changes to 'Y' because none of its adjacent pixels have the same original color.
This problem is a classic application of the Depth-First Search (DFS) pattern in recursion.
The recursive approach naturally handles the exploration of connected pixels.
We need to be careful not to revisit pixels we've already processed to avoid infinite recursion.
The algorithm can also be implemented iteratively using a queue (BFS) or stack (DFS), but the recursive approach is often more intuitive.
This problem has several practical applications:
Paint bucket tools in applications like Photoshop, GIMP, and MS Paint use flood fill algorithms.
Flood fill is used in image segmentation, object detection, and boundary extraction in computer vision.
Used in games for map generation, territory control visualization, and path finding.