robby171 | Salut,
J'ai écrit ce bout de code là:
Code :
- # possible paths are generated by the build function,
- # each position in an external table has a character
- # associated with it. the build function generates
- # possible paths, and will create words based
- # on these paths
- #
- # 0 1 2 3
- # 4 5 6 7
- # 8 9 10 11
- # 12 13 14 15
- def next_map_move(pos):
- """
- This returns the positions that are reachable near to a cell,
- regardless of whether the position has been used before or not.
- """
- row, col = pos/4, pos%4
- temp = set([i*4 + j for i in range(-1+row, 2+row) if i in range(4) for j in range(-1+col, 2+col) if j in range(4)])
- temp.remove(pos)
- return temp
- def next_possible_move(pos):
- """
- This returns the positions that are playable from a cell,
- taking into account that a position that has been used already
- is not usable anymore.
- """
- temp = next_map_move(pos) - been_to_map_move
- if pos in temp: temp.remove(pos)
- return list(temp)
- been_to_map_move = set()
- # this currently displays possible paths given pos and deep
- def build(pos, deep = 3, i = 0):
- been_to_map_move.add(pos)
- if len(next_possible_move(pos)) == 0 or i == deep:
- been_to_map_move.remove(pos)
- return
- print " "*i + "{0}".format(pos)
- for j in next_possible_move(pos):
- build(j, deep = deep, i = i+1)
- been_to_map_move.remove(pos)
|
Le code fonctionne, par exemple:
Code :
- >>> build(3,3)
- 3
- 2
- 1
- 5
- 6
- 7
- 6
- 1
- 2
- 5
- 7
- 9
- 10
- 11
- 7
- 2
- 11
- 10
- 6
|
Mais maintenant je voudrais que le résultat soit retourné sous forme de liste de listes, ou sous forme de liste de tuples. Je sais pas trop comment m'y prendre: tableau extérieur que je complète au fur et à mesure avec des tests (comparer le niveau d'itération à chaque fois, si le niveau augmente j'incrémente dans un sous-tableau, si je reste au même niveau je fais un nouveau sous-tableau copie du précédent sous-tableau avec la dernière position différente, si le niveau baisse je pars sur un nouveau sous-tableau)? Je sais vraiment pas si je devrais faire comme ça, ou comment je devrais faire... Merci... |