##### Welcome to Subscribe On Youtube
• import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.

To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:
Input:
image =
[
[1,1,1],
[1,1,0],
[1,0,1]
]
sr = 1, sc = 1, newColor = 2
Output:
[
[2,2,2],
[2,2,0],
[2,0,1]
]

Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.

Note:

The length of image and image[0] will be in the range [1, 50].
The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

*/
public class Flood_Fill {

public static void main(String[] args) {
Flood_Fill out = new Flood_Fill();
Solution s = out.new Solution();

System.out.println(Arrays.deepToString(s.floodFill(new int[][]{ {0,0,0}, {0,1,0} }, 1, 0, 2)));
}

class Solution {

HashMap<Integer, List<Integer>> isVisited = new HashMap<>();

public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {

if (image == null || image[0] == null) {
return image;
}

dfs(image, sr, sc, newColor, image[sr][sc]);

return image;
}

// corner case: [[0,0,0],[0,1,1]], set [1,1] new color 1 will overlap existing ones
public void dfs(int[][] image, int i, int j, int newColor, int originalColor) {

if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != originalColor || (isVisited.containsKey(i) && isVisited.get(i).contains(j))) {
return;
}

image[i][j] = newColor;

if (isVisited.containsKey(i)) {
isVisited.get(i).add(j);
} else {
List<Integer> list = new ArrayList<>();
list.add(j);
isVisited.put(i, list);
}

dfs(image, i + 1, j, newColor, originalColor);
dfs(image, i - 1, j, newColor, originalColor);
dfs(image, i, j + 1, newColor, originalColor);
dfs(image, i, j - 1, newColor, originalColor);
}
}
}

############

class Solution {
private int[] dirs = {-1, 0, 1, 0, -1};
private int[][] image;
private int nc;
private int oc;

public int[][] floodFill(int[][] image, int sr, int sc, int color) {
nc = color;
oc = image[sr][sc];
this.image = image;
dfs(sr, sc);
return image;
}

private void dfs(int i, int j) {
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc
|| image[i][j] == nc) {
return;
}
image[i][j] = nc;
for (int k = 0; k < 4; ++k) {
dfs(i + dirs[k], j + dirs[k + 1]);
}
}
}

• // OJ: https://leetcode.com/problems/flood-fill/
// Time: O(MN)
// Space: O(MN)
class Solution {
private:
int M, N, color, dirs[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
void dfs(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (sr < 0 || sr >= M || sc < 0 || sc >= N || image[sr][sc] != color) return;
image[sr][sc] = newColor;
for (auto dir : dirs) {
dfs(image, sr + dir[0], sc + dir[1], newColor);
}
}
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
color = image[sr][sc];
if (color == newColor) return image;
M = image.size();
N = image[0].size();
dfs(image, sr, sc, newColor);
return image;
}
};

• class Solution:
def floodFill(
self, image: List[List[int]], sr: int, sc: int, color: int
) -> List[List[int]]:
def dfs(i, j):
if (
not 0 <= i < m
or not 0 <= j < n
or image[i][j] != oc
or image[i][j] == color
):
return
image[i][j] = color
for a, b in pairwise(dirs):
dfs(i + a, j + b)

dirs = (-1, 0, 1, 0, -1)
m, n = len(image), len(image[0])
oc = image[sr][sc]
dfs(sr, sc)
return image

############

class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
SR, SC = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r - 1, c)
if r < SR - 1: dfs(r + 1, c)
if c >= 1: dfs(r, c - 1)
if c < SC - 1: dfs(r, c + 1)
dfs(sr, sc)
return image

• func floodFill(image [][]int, sr int, sc int, color int) [][]int {
oc := image[sr][sc]
m, n := len(image), len(image[0])
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int)
dfs = func(i, j int) {
if i < 0 || i >= m || j < 0 || j >= n || image[i][j] != oc || image[i][j] == color {
return
}
image[i][j] = color
for k := 0; k < 4; k++ {
dfs(i+dirs[k], j+dirs[k+1])
}
}
dfs(sr, sc)
return image
}

• function floodFill(
image: number[][],
sr: number,
sc: number,
newColor: number,
): number[][] {
const m = image.length;
const n = image[0].length;
const target = image[sr][sc];
const dfs = (i: number, j: number) => {
if (
i < 0 ||
i === m ||
j < 0 ||
j === n ||
image[i][j] !== target ||
image[i][j] === newColor
) {
return;
}
image[i][j] = newColor;
dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
};
dfs(sr, sc);
return image;
}


• impl Solution {
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 {
return;
}
let sr = sr as usize;
let sc = sc as usize;
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
return;
}
image[sr][sc] = new_color;
let sr = sr as i32;
let sc = sc as i32;
Self::dfs(image, sr + 1, sc, new_color, target);
Self::dfs(image, sr - 1, sc, new_color, target);
Self::dfs(image, sr, sc + 1, new_color, target);
Self::dfs(image, sr, sc - 1, new_color, target);
}
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
let target = image[sr as usize][sc as usize];
Self::dfs(&mut image, sr, sc, new_color, target);
image
}
}


• class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int prevColor = image[sr][sc];
if (newColor == prevColor)
return image;
int rows = image.length, columns = image[0].length;
int[][] newImage = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++)
newImage[i][j] = image[i][j];
}
newImage[sr][sc] = newColor;
Queue<int[]> queue = new LinkedList<int[]>();
queue.offer(new int[]{sr, sc});
int[][] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
while (!queue.isEmpty()) {
int[] pixel = queue.poll();
int row = pixel[0], column = pixel[1];
for (int[] direction : directions) {
int newRow = row + direction[0], newColumn = column + direction[1];
if (newRow >= 0 && newRow < rows && newColumn >= 0 && newColumn < columns && newImage[newRow][newColumn] == prevColor) {
newImage[newRow][newColumn] = newColor;
queue.offer(new int[]{newRow, newColumn});
}
}
}
return newImage;
}
}

############

class Solution {
private int[] dirs = {-1, 0, 1, 0, -1};
private int[][] image;
private int nc;
private int oc;

public int[][] floodFill(int[][] image, int sr, int sc, int color) {
nc = color;
oc = image[sr][sc];
this.image = image;
dfs(sr, sc);
return image;
}

private void dfs(int i, int j) {
if (i < 0 || i >= image.length || j < 0 || j >= image[0].length || image[i][j] != oc
|| image[i][j] == nc) {
return;
}
image[i][j] = nc;
for (int k = 0; k < 4; ++k) {
dfs(i + dirs[k], j + dirs[k + 1]);
}
}
}

• // OJ: https://leetcode.com/problems/flood-fill/
// Time: O(MN)
// Space: O(MN)
class Solution {
private:
int M, N, color, dirs[4][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
void dfs(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (sr < 0 || sr >= M || sc < 0 || sc >= N || image[sr][sc] != color) return;
image[sr][sc] = newColor;
for (auto dir : dirs) {
dfs(image, sr + dir[0], sc + dir[1], newColor);
}
}
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
color = image[sr][sc];
if (color == newColor) return image;
M = image.size();
N = image[0].size();
dfs(image, sr, sc, newColor);
return image;
}
};

• class Solution:
def floodFill(
self, image: List[List[int]], sr: int, sc: int, color: int
) -> List[List[int]]:
def dfs(i, j):
if (
not 0 <= i < m
or not 0 <= j < n
or image[i][j] != oc
or image[i][j] == color
):
return
image[i][j] = color
for a, b in pairwise(dirs):
dfs(i + a, j + b)

dirs = (-1, 0, 1, 0, -1)
m, n = len(image), len(image[0])
oc = image[sr][sc]
dfs(sr, sc)
return image

############

class Solution(object):
def floodFill(self, image, sr, sc, newColor):
"""
:type image: List[List[int]]
:type sr: int
:type sc: int
:type newColor: int
:rtype: List[List[int]]
"""
SR, SC = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: dfs(r - 1, c)
if r < SR - 1: dfs(r + 1, c)
if c >= 1: dfs(r, c - 1)
if c < SC - 1: dfs(r, c + 1)
dfs(sr, sc)
return image

• func floodFill(image [][]int, sr int, sc int, color int) [][]int {
oc := image[sr][sc]
m, n := len(image), len(image[0])
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int)
dfs = func(i, j int) {
if i < 0 || i >= m || j < 0 || j >= n || image[i][j] != oc || image[i][j] == color {
return
}
image[i][j] = color
for k := 0; k < 4; k++ {
dfs(i+dirs[k], j+dirs[k+1])
}
}
dfs(sr, sc)
return image
}

• function floodFill(
image: number[][],
sr: number,
sc: number,
newColor: number,
): number[][] {
const m = image.length;
const n = image[0].length;
const target = image[sr][sc];
const dfs = (i: number, j: number) => {
if (
i < 0 ||
i === m ||
j < 0 ||
j === n ||
image[i][j] !== target ||
image[i][j] === newColor
) {
return;
}
image[i][j] = newColor;
dfs(i + 1, j);
dfs(i - 1, j);
dfs(i, j + 1);
dfs(i, j - 1);
};
dfs(sr, sc);
return image;
}


• impl Solution {
fn dfs(image: &mut Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32, target: i32) {
if sr < 0 || sr == image.len() as i32 || sc < 0 || sc == image[0].len() as i32 {
return;
}
let sr = sr as usize;
let sc = sc as usize;
if sr < 0 || image[sr][sc] == new_color || image[sr][sc] != target {
return;
}
image[sr][sc] = new_color;
let sr = sr as i32;
let sc = sc as i32;
Self::dfs(image, sr + 1, sc, new_color, target);
Self::dfs(image, sr - 1, sc, new_color, target);
Self::dfs(image, sr, sc + 1, new_color, target);
Self::dfs(image, sr, sc - 1, new_color, target);
}
pub fn flood_fill(image: Vec<Vec<i32>>, sr: i32, sc: i32, new_color: i32) -> Vec<Vec<i32>> {
let target = image[sr as usize][sc as usize];
Self::dfs(&mut image, sr, sc, new_color, target);
image
}
}