I’m trying to apply the flood fill on a specific question. I’m having a hard time understanding how to create a variable with a help function so I keep it throughout all the recursion.
I’ve seen there is a thread about it though I don’t know how to apply it for my own use.
This is the code I wrote so far:
how do I create the k variable to stay the same?
public static int[][] fill(int[][] map, int i, int j, int color) {
int[][] ans = null;
if ((i<map.length)&&(i>0)&&(j<map.length)&&(j>0))
{
if ((k!=map[i][j]))
{
map[i][j]=color;
}
}
fill(map,i-1,j,color);
fill(map,i+1,j,color);
fill(map,i,j-1,color);
fill(map,i,j+1,color);
return ans;
}
public static void fill(int[][] map, int i, int j, int color,int k)
k=map[i][j]
lets say this is my array:
4, 1, 2, 2
4, 4, 3, 1
1, 4, 4, 4
1, 4, 0, 2
and I wish to flood fill all the indexes that have the value of 4.
I wish k to obtain the value of 4 so I can use k to compare in each index.
Iמ Addition to your question i have noticed that your function wont work because you boundries checking, you are check if row or col are equal to the board length , its alright but you need to change it to <, and not <= . anyway you can use your code like that :
for checking :
Good Luck!