|
-
May 10th, 2010, 12:39 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Converting recursion to iteration
Is it possible to make this function iterative or must it be recursive?
The Function traverses a grid of x length and y height. At each point in the grid, it checks all of it's neighbors to see if they are valid, meaning they exist in the grid and haven't been used yet.
The function works in it's current form but I was wondering if an iterative version would be more efficient.
PathManip is a Stringbuilder, stores the path currently being manipulated.
PathQueue is a List(Of String), stores all paths yet to be traversed.
IsPathAlreadyUsed is a boolean function that checks whether or not a specified point has been used in the current path.
Code:
Sub FindNeighbors(ByVal coords As String)
Dim x As Integer = CInt(coords(coords.Length - 2).ToString)
Dim y As Integer = CInt(coords(coords.Length - 1).ToString)
PathManip.Clear()
PathManip.Append(coords)
PathQueue.RemoveAt(0) ' Remove the path from the queue since we are traversing it now.
'Up
If y - 1 >= 0 AndAlso Not IsPathAlreadyUsed(coords, x, y - 1) Then
PathManip.Append(x & y - 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Up-right
If y - 1 >= 0 AndAlso x + 1 <= xLimit AndAlso Not IsPathAlreadyUsed(coords, x + 1, y - 1) Then
PathManip.Append(x + 1 & y - 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Right
If x + 1 <= xLimit AndAlso Not IsPathAlreadyUsed(coords, x + 1, y) Then
PathManip.Append(x + 1 & y)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Down-right
If x + 1 <= xLimit AndAlso y + 1 <= yLimit AndAlso Not IsPathAlreadyUsed(coords, x + 1, y + 1) Then
PathManip.Append(x + 1 & y + 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Down
If y + 1 <= yLimit AndAlso Not IsPathAlreadyUsed(coords, x, y + 1) Then
PathManip.Append(x & y + 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Down-left
If x - 1 >= 0 AndAlso y + 1 <= yLimit AndAlso Not IsPathAlreadyUsed(coords, x - 1, y + 1) Then
PathManip.Append(x - 1 & y + 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Left
If x - 1 >= 0 AndAlso Not IsPathAlreadyUsed(coords, x - 1, y) Then
PathManip.Append(x - 1 & y)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' Up-left
If x - 1 >= 0 AndAlso y - 1 >= 0 AndAlso Not IsPathAlreadyUsed(coords, x - 1, y - 1) Then
PathManip.Append(x - 1 & y - 1)
If CheckWord(PathManip) Then
PathQueue.Add(PathManip.ToString)
End If
PathManip.Remove(PathManip.Length - 2, 2)
End If
' If there are paths in the queue, traverse them.
While PathQueue.Count > 0
FindNeighbors(PathQueue(0))
End While
End Sub
Prefix has no suffix, but suffix has a prefix.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|