J'essaie de me familiariser avec python. Je pensais résoudre ce puzzle de chameaux. C'est le code que j'ai jusqu'ici. J'ai quelques problèmes maintenant:Comment procéder avec ce programme python
fCamel = 'F'
bCamel = 'B'
gap = 'G'
def solution(formation):
return len([i for i in formation[formation.index(fCamel) + 1:] if i == bCamel]) == 0
def heuristic(formation):
fCamels, score = 0, 0
for i in formation:
if i == fCamel:
fCamels += 1;
elif i == bCamel:
score += fCamels;
else:
pass
return score
def getneighbors (formation):
igap = formation.index(gap)
res = [[]]
# AB_CD --> A_BCD | ABC_D | B_ACD | ABD_C
def genn(i,j):
temp = list(formation)
temp[i], temp[j] = temp[j], temp[i]
res.append(temp)
if(igap > 0):
genn(igap, igap-1)
if(igap > 1):
genn(igap, igap-2)
if igap < len(formation) - 1:
genn(igap, igap+1)
if igap < len(formation) - 2:
genn(igap, igap+2)
return res
def astar (formation, heuristicf, solutionf, getneighborsf):
openlist = [].append(formation)
closedlist = []
#Example usage (I think)
#astar([fCamel, fCamel, fCamel, gap, bCamel, bCamel, bCamel], heuristic, solution, getneighbors)
J'ai quelques problèmes maintenant.
- Je dois avoir 3 champs de données supplémentaires avec une formation. g = distance actuelle, f = valeur totale (valeur heuristique + g), p = parent. Comment faire une structure incluant tous ces éléments?
- Je dois être en mesure de déterminer si une formation donnée est dans la liste fermée. Efficacement. Comment faire ça?