Ich habe ein Programm suchen den kürzesten Weg in einem Raster von einer Position zur anderen.Gibt es eine Möglichkeit, alle möglichen Anweisungen von Anweisungen auszuführen?
Ich denke, der kürzeste zurückgegebene Pfad hängt davon ab, welche Richtung im Array die Funktion zuerst aufruft.
Gibt es eine Möglichkeit, dieselbe Funktion wie unten auszuführen, außer einmal für jede mögliche Reihenfolge von Norden, Süden, Osten, Westen?
Ich verstehe, dass ich 4 haben könnte! ähnliche Funktionen, aber ich frage mich, ob es einen saubereren Weg gibt, es zu tun.
Entschuldigung im Voraus für Ignoranz meinerseits, bin ich nicht besonders erfahren!
def find_path(world, path, robotx, roboty, goalx, goaly, size):
print("Robot at: ", robotx, ",", roboty)
#print(path)
time.sleep(0.7)
if [robotx, roboty] == [goalx, goaly]:
path_count = 0
print("Woohoo! Goal found at ", goalx, ',', goaly, '. ', "Steps taken: ", path_count)
print(path)
return path
else:
#South
if robotx != size and world[robotx + 1][roboty] in (0, 2):
world[robotx + 1][roboty] = 3
path.add_head(Node([robotx + 1, roboty]))
find_path(world, path, robotx + 1, roboty, goalx, goaly, size)
#East
if roboty != size and world[robotx][roboty + 1] in (0, 2):
world[robotx][roboty + 1] = 3
path.add_head(Node([robotx, roboty + 1]))
find_path(world, path, robotx, roboty + 1, goalx, goaly, size)
#North
if robotx != 0 and world[robotx - 1][roboty] in (0, 2):
world[robotx - 1][roboty] = 3
path.add_head(Node([robotx - 1, roboty]))
find_path(world, path, robotx - 1, roboty, goalx, goaly, size)
#West
if roboty != 0 and world[robotx][roboty - 1] in (0, 2):
world[robotx][roboty - 1] = 3
path.add_head(Node([robotx, roboty - 1]))
find_path(world, path, robotx, roboty - 1, goalx, goaly, size)
ich denke, es sollte 'checkDir sein (1,0)', 'checkDir (-1,0)', 'checkDir (0,1) 'und' checkDir (0, -1) '. Um auch aus allen Permutationen zu suchen und auszuwählen, beachte 'liste (itertools.permutation ([(1,0), (-1,0), (0,1), (0, -1)])). –