Skip to content

Instantly share code, notes, and snippets.

@optimistiks
Created July 6, 2023 22:02
Show Gist options
  • Select an option

  • Save optimistiks/1b403b67766607433b3ca308bcdf2801 to your computer and use it in GitHub Desktop.

Select an option

Save optimistiks/1b403b67766607433b3ca308bcdf2801 to your computer and use it in GitHub Desktop.
Our task is to perform flood fill by updating the values of the four directionally connected cells, which have the same value as the source cell with the target value.
function floodFill(grid, sr, sc, target) {
// last argument is the original value that was in the source cell
// (which is the value that an adjacent cell must have for the fill to continue)
// since its our first call we pass null
// so this value can be initialized
floodFillRec(grid, sr, sc, target, null)
return grid
}
function floodFillRec(grid, sr, sc, target, prevV) {
if (grid[sr] == null || grid[sr][sc] == null) {
// check if we went out of bounds
return;
}
// keep the original source cell value to check all connected cells
const prev = prevV ?? grid[sr][sc];
if (grid[sr][sc] !== prev || grid[sr][sc] === target) {
// either value in the cell is not equal to the initial source cell value,
// or value is the target already
// in any case, return
return
}
// replace value in the grid
grid[sr][sc] = target;
// branch out to the 4 connected cells, left / right and top / bottom
floodFillRec(grid, sr + 1, sc, target, prev)
floodFillRec(grid, sr - 1, sc, target, prev)
floodFillRec(grid, sr, sc + 1, target, prev)
floodFillRec(grid, sr, sc - 1, target, prev)
}
export { floodFill };
// depth of the recursion tree is n / 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment