我想與python熟悉。以爲我會解決這個駱駝謎題。這是迄今爲止的代碼。我現在有幾個問題:如何繼續這個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)
我現在有幾個問題。
- 我需要3個數據字段以及一個編組。 g =當前距離,f =總值(啓發式值+ g),p =父項。如何製作一個包含所有這些的結構?
- 我需要能夠確定給定的陣型是否在封閉列表中。有效率的。這個怎麼做?