Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created January 16, 2017 17:44
Show Gist options
  • Select an option

  • Save deyindra/23fced141ce76491600a4f1ccc81edba to your computer and use it in GitHub Desktop.

Select an option

Save deyindra/23fced141ce76491600a4f1ccc81edba to your computer and use it in GitHub Desktop.
Flood Fill Algorithm
private static <T> boolean isSafe(T[][] array, int rowIndex, int colIndex,Predicate<T> predicate){
final int ROW = array.length;
final int COL = array[0].length;
return rowIndex>=0 && rowIndex<ROW && colIndex>=0 && colIndex<COL
&& predicate.test(array[rowIndex][colIndex]);
}
public static <T> void floodFill(T[][] array, int rowIndex, int colIndex, T newObject, Predicate<T> predicate){
int rowNbr[] = new int[] {-1, -1, -1, 0, 0, 1, 1, 1};
int colNbr[] = new int[] {-1, 0, 1, -1, 1, -1, 0, 1};
if(!isSafe(array,rowIndex,colIndex,predicate)){
return;
}
array[rowIndex][colIndex]=newObject;
for(int i=0;i<8;i++){
floodFill(array,rowIndex+rowNbr[i], colIndex+colNbr[i],newObject, predicate);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment