Implement the "paint fill" function of image editing programs: Given a screen point and new color fill the surrounding area.
Any comments on the code below?
Time complexity: \$\mathcal{O}(N^2)\$
Space complexity: \$\mathcal{O}(N^2)\$
static int N = 6;
static boolean[][] colored = new boolean[N+2][N+2];
static int old_color;
public static void fillP(int[][] array, int x, int y, int color){
System.out.println("Before");
printArray2D(array);
old_color = array[x][y];
fillPaint(array, x, y, color);
System.out.println("After");
printArray2D(array);
}
public static void fillPaint(int[][] array, int x, int y, int color){
if (x < 0 || x > N || y < 0 || y > N) return;
if (array[x][y] != old_color) return;
array[x][y] = color;
colored[x+1][y+1] = true;
for (int i = -1; i < 2; i += 2){
for (int j = -1; j < 2; j += 2){
if (!colored[x+i+1][y+1]){
fillPaint(array, x+i, y, color);
}
if (!colored[x+1][y+j+1]){
fillPaint(array, x, y+j, color);
}
}
}
}
public static void printArray2D(int[][] array){
int n = array.length;
int m = array[0].length;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (j == m-1){
System.out.print(array[i][j] + " ");
System.out.println();
}
else{
System.out.print(array[i][j] + " ");
}
}
}
}
}