私は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 =親。これらすべてを含む構造を作るには?
- 指定されたフォーメーションが閉じたリストにあるかどうかを判断できる必要があります。効率的に。これを行う方法?