Original Post
Hello people , I need some help with a recursion code . The task is : I have a NxN board ,filled with numbers between [0-9]. When the user writes : " public static int[][] fill(int[][] map, int x, int y, int cc) " the board ,from the coordinate (x,y) , would start feeling each relative coordinate to (x,y) , with the number that stored in the variable "cc" I wrote the code , but I have a non ending loop and i can't figure out what to do.I've tried change it over and over again , but nothing. I'll appreciate any help or hint you can add , thanks in advance . Here is the code : the non recurcive method "fill" calls the recurcive method "fill_And_Not_Return" . The "not1,not2" variables resresents the previous coordinate that "I was" , so when I go to its next neighbor , I will not go again to the coordinate which I was at . Can I add pictures in this forum ? to clarify the problem ?
public static int[][] fill(int[][] map, int x, int y, int cc)
{
int n = map.length - 1;
fill_And_Not_Return(map,x,y,-1,-1,n,cc);
return map;
}
public static void fill_And_Not_Return (int [][]map,int x ,int y,int not1,int not2,int n,int cc)
{
if (x==0 & y==0) // left up corner - MAX 2 neighbors
{
if (0!=not1 |1!=not2 && legalNeighbor(map,x,y,0,1)==true)
fill_And_Not_Return(map,0,1,x,y,n,cc);
if (1!=not1 | 0!=not2 && legalNeighbor(map,x,y,1,0)==true)
fill_And_Not_Return(map,1,0,x,y,n,cc);
map[x][y]=cc;
}
if (y==0 & x>0 & x<n) // the middle of the first line - MAX 3 neighbors
{
if (x-1!=not1| y!=not2 && legalNeighbor(map,x,y,x-1,y)==true)
fill_And_Not_Return(map,x-1,y ,x,y,n,cc);
if (x!=not1 | y+1!=not2 && legalNeighbor(map,x,y,x,y+1)==true)
fill_And_Not_Return(map,x,y+1 ,x,y,n,cc);
if (x+1!=not1 | y!=not2 && legalNeighbor(map,x,y,x+1,y)==true)
fill_And_Not_Return(map,x+1,y ,x,y,n,cc);
map[x][y]=cc;
}
if (y>0 & y<n & x==0) // left column - MAX 3 neighbors (not the corners!!!)
{
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
if (x+1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x+1,y)==true)
fill_And_Not_Return(map,x+1,y ,x,y,n,cc);
if (x!=not1 | y+1!=not2 && legalNeighbor(map,x,y ,x,y+1)==true)
fill_And_Not_Return(map,x,y+1 ,x,y,n,cc);
map[x][y]=cc;
}
if (y>0 & y<n & x==n) // right column - MAX 3 neighbors (not the corners!!!)
{
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
if (x-1!=not1 |y!=not2 && legalNeighbor(map,x,y ,x-1,y)==true)
fill_And_Not_Return(map,x-1,y ,x,y,n,cc);
if (x!=not1 | y+1!=not2 && legalNeighbor(map,x,y ,x,y+1)==true)
fill_And_Not_Return(map,x,y+1 ,x,y,n,cc);
map[x][y]=cc;
}
if (y==n & x==0) // down left corner - 2 MAX
{
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
if (x+1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x+1,y)==true)
fill_And_Not_Return(map,x+1,y ,x,y,n,cc);
map[x][y]=cc;
}
if (x==n & y==n) // down right corner - 2 MAX
{
if (x-1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x-1,y)==true)
fill_And_Not_Return(map,x-1,y ,x,y,n,cc);
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
map[x][y]=cc;
}
if ( y==n & x>0 & x<n ) // the middle of the last line // 3 MAX
{
if (x-1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x-1,y)==true)
fill_And_Not_Return(map,x-1,y ,x,y,n,cc);
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
if (x+1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x+1,y)==true)
fill_And_Not_Return(map,x+1,y ,x,y,n,cc);
map[x][y]=cc;
}
/* If the program got here ,the point is somethere inside the matrix */
if (x>0 && x<n && y>0 && y<n)
{
if (x-1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x-1,y)==true)
fill_And_Not_Return(map,x-1,y,x,y,n,cc);
if (x!=not1 | y-1!=not2 && legalNeighbor(map,x,y ,x,y-1)==true)
fill_And_Not_Return(map,x,y-1 ,x,y,n,cc);
if (x+1!=not1 | y!=not2 && legalNeighbor(map,x,y ,x+1,y)==true)
fill_And_Not_Return(map,x+1,y ,x,y,n,cc);
if (x!=not1 | y+1!=not2 && legalNeighbor(map,x,y ,x,y+1)==true)
fill_And_Not_Return(map,x,y+1 ,x,y,n,cc);
map[x][y]=cc;
}
} // end of fill_and_not_return