Created
January 16, 2017 17:44
-
-
Save deyindra/23fced141ce76491600a4f1ccc81edba to your computer and use it in GitHub Desktop.
Flood Fill Algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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