我想与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 =父项。如何制作一个包含所有这些的结构?
- 我需要能够确定给定的阵型是否在封闭列表中。有效率的。这个怎么做?