![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Avoiding Stackoverflow error using recursion to solve a maze
I've been solving many maze problems lately. I've been doing this mainly to get myself better acquainted with recursion as earlier it used to be quite intimidating to me, and I have solved way too many maze problems using loops where in hindsight it would have been more efficient to use recursion. On almost every problem I solve I end up getting a StackOverflowException. After pinpointing the problem, I notice that it keeps jumping between two spots in my matrix. I have solved this by passing two new parameters to my method: the the old x and y coordinates and this way I make sure that my recursive method never goes backwards, so it doesn't jump between two points. This solution works, but I was wondering if there was a more elegant solution to solving my problem.
Thanks in advance.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
You could have an extra boolean in your matrix that says whether a location has been "visited". When you try to go on to a visited location, just treat it as unpassable.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|