Algo-Sim › Grid Graphs
Flood Fill
Easy · Grid Graphs · step through it in Java, Python and C++ on your own input
Repaint the pixel (sr, sc) and every pixel of the same colour connected to it with a new colour.
Problem
An image is a grid of colour values. Starting from pixel (sr, sc), repaint it and every pixel connected to it up, down, left or right through pixels of the same original colour, using color. Return the image.
Constraints
- 1 ≤ m, n ≤ 50
- 0 ≤ image[i][j], color < 2¹⁶
- If the new colour equals the old one, nothing changes (and a naive fill loops forever)
Edge cases to test
- The new colour equals the old one (must not loop forever)
- A 1 × 1 image
- The region touches every border
Follow-up questions
- Why check old == new first?
- Otherwise repainted cells still match the old colour and the fill never stops.
- How do you avoid deep recursion?
- Use an explicit stack or a queue; a large region can overflow the call stack.
Solution
Java
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
int original = image[sr][sc];
if (original != color) dfs(image, sr, sc, original, color);
return image;
}
private void dfs(int[][] image, int r, int c, int original, int color) {
if (r < 0 || c < 0 || r >= image.length || c >= image[0].length || image[r][c] != original) return;
image[r][c] = color;
dfs(image, r + 1, c, original, color);
dfs(image, r - 1, c, original, color);
dfs(image, r, c + 1, original, color);
dfs(image, r, c - 1, original, color);
}Python
def flood_fill(image, sr, sc, color):
original = image[sr][sc]
if original != color:
dfs(image, sr, sc, original, color)
return image
def dfs(image, r, c, original, color):
if not (0 <= r < len(image) and 0 <= c < len(image[0])) or image[r][c] != original:
return
image[r][c] = color
dfs(image, r + 1, c, original, color)
dfs(image, r - 1, c, original, color)
dfs(image, r, c + 1, original, color)
dfs(image, r, c - 1, original, color)C++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int color) {
int original = image[sr][sc];
if (original != color)
dfs(image, sr, sc, original, color);
return image;
}
void dfs(vector<vector<int>> &image, int r, int c, int original, int color) {
if (r < 0 || c < 0 || r >= int(image.size()) || c >= int(image[0].size()) || image[r][c] != original)
return;
image[r][c] = color;
dfs(image, r + 1, c, original, color);
dfs(image, r - 1, c, original, color);
dfs(image, r, c + 1, original, color);
dfs(image, r, c - 1, original, color);
}
};
More Grid Graphs problems
- Number of Islands (BFS) (Medium)
- Number of Islands (DFS) (Medium)
- Max Area of Island (Medium)
- Rotting Oranges (Medium)
- 01 Matrix (Medium)
- Walls and Gates (Medium)
- Shortest Path in Binary Matrix (Medium)
- Shortest Bridge (Medium)
- Shortest Path in a Grid with Obstacles Elimination (Hard)
- Surrounded Regions (Medium)
- Pacific Atlantic Water Flow (Medium)
- Path With Minimum Effort (Medium)
- Minimum Cost to Make at Least One Valid Path in a Grid (Hard)
- Swim in Rising Water (Hard)