0

Cant remove stackOverflowError in my code of getMazePaths...

Hello, So I am getting stackoverflow error in my code but I cant debug it, I have put termination condition as well yet hard luck. Link to the code is: https://pastebin.com/VFZ63v7a

26th Jul 2020, 4:06 AM
Ashish
Ashish - avatar
6 ответов
+ 1
getMazePaths(int sr, int sc, int dr, int dc) { .. if (sr < dr){ hpaths = getMazePaths(sr, sc +1, dr, dc); this never stops: 1,1, 5,5 if(1<5) do 1, 1+1, 5,5 1,2, 5,5 if(1<5) do 1, 2+1, 5,5 1,3, 5,5 if(1<5) do 1, 3+1, 5,5 1,4, 5,5 if(1<5) do 1, 4+1, 5,5 1,5, 5,5 if(1<5) do 1, 5+1, 5,5
26th Jul 2020, 11:30 AM
zemiak
+ 1
By these statements, you are repeated calling getMazePaths function infinitely.. So it's causing stack flow error... if (sr < dr){ hpaths = getMazePaths(sr, sc + 1, dr, dc); } if(sc < dc){ vpaths = getMazePaths(sr + 1, sc, dr, dc); } Where termination condition you put?
26th Jul 2020, 11:23 AM
Jayakrishna 🇮🇳
0
if (sr == dr && sc == dc){             ArrayList<String> bres = new ArrayList<>();             bres.add("");             return bres;         } This is the terminating condition.
26th Jul 2020, 11:25 AM
Ashish
Ashish - avatar
0
&& will not work because, until if input numbers n, m are same I think, because either you increasing sr or sc, and using && so if it has to stop, n shloud be equal to m. So use || opearator , like if (sr == dr || sc == dc){ //use || instead of &&. ArrayList<String> bres = new ArrayList<>(); bres.add(""); return bres; }
26th Jul 2020, 11:29 AM
Jayakrishna 🇮🇳
0
Thus condition is there bcoz if we reach there to the last index that is ( 2, 2), then only we will terminate and return What you did would be failing if any of sr or sc becomes equal to sc or dc. Like 1,2 and you will enter inside if condition.
26th Jul 2020, 11:34 AM
Ashish
Ashish - avatar
0
zemiak yeah gotcha bruh, thanks.. :)
26th Jul 2020, 11:37 AM
Ashish
Ashish - avatar