![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Recursive Function For Testing A Connection?
I have a two dimensional grid, and need to test if any given point on the grid is connected to any side of the grid, through any other combination of points. Would a recursive function be the best way to do this? If so, how would I do that without ending up with infinite recursion?
If recursion isn't the best option, my other thought was some straight loops, but I'm getting tired of that crap. ![]() Edit: Wrong forum. I meant to put this in "Other Scripting Languages", not "Other Web Development". Move please. :o |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
I think recursion would be the best way to go about this, as you say. To avoid infinite recursion, which I presume would occur when connections loop back on themselves, just log where you've been.
This sounds like fun. Time to break out Python. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
All right. I took a buddy's suggestion and just did iteration from bottom left to top right. Any point that's connected to a wall is deemed connected, and any point that's connected to a point deemed connected... is connected. Then an outside loop goes around that to work itself backwards, and bingo. It worked perfectly. I don't think recursion would have worked as well in this instance.
|
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Makes sense to me. I didn't realise "connected" was defined as being attached to a wall - should've re-read your post. For some reason, I thought you wanted a function to check if a node was connected to another node.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I thought you meant Beej-y connections. Go to your room. I get your dessert.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|