Voici le début d' une teste en PureBasic pour trouver la sortie
Analyse
Résultat (note une pause est volontairement faite)
Télécharger le code
L'algo en lui même
Code : Tout sélectionner
Procedure findPath (*this._struct,*nodeFrom,*nodeTo,Array cardinal(1),*callBack)
With *this
Protected.Node::Node currentNode = *nodeFrom,neighbor
Protected NewMap visitedNodes.Node::Node()
ClearList(\path())
Protected currentPoint.l,neighborIsFound.b
; we start with the from node
AddMapElement(visitedNodes(),Str(currentNode)):visitedNodes() = currentNode
AddElement(\path()): \path() = currentNode
; loop until we don't find the target and we have possibility
Repeat
; look for a neighbor
neighborIsFound = #False ;we start without neighbor found
For currentPoint=0 To ArraySize(cardinal())-1
neighbor = currentNode\getPath(cardinal(currentPoint))
If neighbor ; a neighbor is find
; if the node has not be already visited
If Not FindMapElement(visitedNodes(),Str(neighbor))
; we take this node
currentNode = neighbor
AddMapElement(visitedNodes(),Str(currentNode)):visitedNodes() = currentNode
AddElement(\path()):\path() = currentNode
neighborIsFound = #True
; we call callback function if it exists
If *callBack
CallFunctionFast(*callBack,currentNode)
EndIf
Break ;don't search other neighbor
EndIf
EndIf
Next
If Not neighborIsFound ; no neighbor has not be found
; we go back to back and remove it from path
If LastElement(\path())
currentNode = \path()
DeleteElement(\path())
If *callBack
CallFunctionFast(*callBack,currentNode)
EndIf
EndIf
EndIf
Until (currentNode = *nodeTo) And (ListSize(\path()))
If ListSize(\path())
ProcedureReturn #True ; a path has be found
EndIf
ProcedureReturn #False ;no path has be not found
EndWith
EndProcedure