Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2006, 9:56 PM   #1
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Feb 9th, 2006, 10:01 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:39 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC