Suppose there is a 2d matrix n by n such that majority of the ints inside the matrix is 0 while the leftovers are 1s.

E.G 10 by 10, with 20 randomly placed 1s and other are all 0s.

I want to use tree recursion to find out the number and the depth of branch for all the 1s. (top/down/left/right/diagonal)

E.g

int m[3][3] = 0;

m[0][0] = 1

m[0][1] = 1

Number of tree = 1, depth of branch = 2.

My problem.

So bascially my code is in an infinite loop. Since I am using tree recursion, my code is bouncing on the same branch, take the above example for example. My code check whether m[0][0] has a 1 if true then use recursion and check all the unit beside it, then it detect (0,1) have a 1 -> proceed to check all the unit beside it, and found out (0,0) is a branch -> (0,1) ->(0,0) infinite XD.

E.G 10 by 10, with 20 randomly placed 1s and other are all 0s.

I want to use tree recursion to find out the number and the depth of branch for all the 1s. (top/down/left/right/diagonal)

E.g

int m[3][3] = 0;

m[0][0] = 1

m[0][1] = 1

Number of tree = 1, depth of branch = 2.

My problem.

So bascially my code is in an infinite loop. Since I am using tree recursion, my code is bouncing on the same branch, take the above example for example. My code check whether m[0][0] has a 1 if true then use recursion and check all the unit beside it, then it detect (0,1) have a 1 -> proceed to check all the unit beside it, and found out (0,0) is a branch -> (0,1) ->(0,0) infinite XD.

Last edited on

Option 1: Do not allow 'left', 'up'.

Option 2: Keep track of nodes that you have already visited so that you won't revisit them.

Option 2: Keep track of nodes that you have already visited so that you won't revisit them.

Topic archived. No new replies allowed.