Flood fill算法是从一个区域中提取若干个连通的点与其他相邻区域区分开的经典算法。因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。在GNU Go和扫雷中,Flood Fill算法被用来计算需要被清除的区域。
https://www.educative.io/edpresso/what-is-the-flood-fill-algorithm
The flood fill algorithm is used to determine the properties of the area around a particular cell in a block. The algorithm is implemented in the bucket fill tool of the Microsoft Paint program(桶填充), which colors the similarly colored pixels, or cells, with another color.
//Flood fill algorithm implemented in Java class Example { public static int M=8; public static int N=8; static void floodFill(int [][] myScreen, int x, int y, int currColor, int newColor) { // Base cases if (x < 0 || x >= Example.M || y < 0 || y >= Example.N) return; if (myScreen[x][y] != currColor) return; if (myScreen[x][y] == currColor) return; // Replace the color at cell (x, y) myScreen[x][y] = newColor; // Recursively call for north, east, south and west floodFill(myScreen, x+1, y, currColor, newColor); floodFill(myScreen, x-1, y, currColor, newColor); floodFill(myScreen, x, y+1, currColor, newColor); floodFill(myScreen, x, y-1, currColor, newColor); } static void findColor(int [][] myScreen, int x, int y, int newColor) { int currColor = myScreen[x][y]; floodFill(myScreen, x, y, currColor, newColor); } public static void main( String args[] ) { int [][] myScreen= new int [][] { {3, 2, 1, 1, 1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 0, 0}, {1, 0, 0, 1, 1, 0, 1, 1}, {1, 2, 2, 2, 2, 0, 1, 0}, {1, 1, 1, 2, 2, 0, 1, 0}, {1, 1, 1, 2, 2, 2, 2, 0}, {1, 1, 1, 1, 1, 2, 1, 1}, {1, 1, 1, 1, 1, 2, 2, 1}, }; //printing initial screen System.out.println("Initial myScreen : "); for (int i=0; i<Example.M; i++) { for (int j=0; j<Example.N; j++) { System.out.print(myScreen[i][j] + " "); } System.out.print("\n"); } int x = 4, y = 4, newColor = 3; findColor(myScreen, x, y, newColor); System.out.print("\n"); System.out.println("Updated myScreen : "); //printing updatedscreen for (int i=0; i<Example.M; i++) { for (int j=0; j<Example.N; j++) { System.out.print(myScreen[i][j] + " "); } System.out.print("\n"); } } }see the programme in geeksfoegeeks: https://www.geeksforgeeks.org/flood-fill-algorithm-implement-fill-paint/
(x,y)变颜色,(x,y)周围的像素也变色