recursive alg question

ok, i have a 8x16 grid of ints. if the grid has a 3 it is an empty space. if it has a 0 it is what im looking for, and if it has a 1 then its a path, basically

33333333

33333333

33333333

33333333

33333333

33333133

33331133

33311333

33311333

33303333

33333333

33333333

31333333

31133333

33113333

33333333

ok so grid[0][0] is 3... and grid[9][3] = 0its grid[y][x]..

you pass the function a cell that is a 1 in the grid and it checks to see if there is a path to a 0 only using 1's.. soo if you pass any of the cells through the function on the top they should return 1 and if you try to run the function on any of the lower set of 1's it would return 0 because there is no 0 connected to them by 1's

I tried setting up a recursive solution to this..

looks like this:

findAZero(int xCoord,int yCoord)

{

int left=0;

int right=0;

int down=0;

if(xCoord<min || xCoord >max)

{return 0}

if(yCoord<min || yCoord > max)

{return 0}

if(grid[y][x] == 0

{

return 1;

}

if(grid[y][x]==3)

{

return 0;

}

if(grid[y][x]==1)

{

left= findAZero(xCoord-1,yCoord);

right=findAZero(xCood+1,yCoord);

down=findAZero(xCoord,yCoord+1);

}

return left+right+down;

}

doesnt work how I want it, can i get some help/tips/suggestions

thanks

[1508 byte] By [JJPeerlessa] at [2007-10-2 13:13:10]
# 1

You need to sit down and think how you would do it yourself rather than just take stabs in the dark. Imagine that your friend is looking at the map and you are not. You can only communicate by asking simple, concrete questions like "Is there a 1 above that?"

Then devise some pseudocode before trying to code the actual code. I will do so myself.

Drake

Drake_Duna at 2007-7-13 10:42:41 > top of Java-index,Other Topics,Algorithms...
# 2
Hint 1) You need a way to prevent running in circles back to the same 1.Hint 2) You have direct access to the array, and so can copy it.Drake
Drake_Duna at 2007-7-13 10:42:41 > top of Java-index,Other Topics,Algorithms...
# 3
yea, dont worry about it, i was thinking about it wrongi actually wanted to pass the function the location of a 0 and find all the 1's recursively.. i got it working thothanks anyways.
JJPeerlessa at 2007-7-13 10:42:41 > top of Java-index,Other Topics,Algorithms...