0

再帰関数を終了する方法を見つけようとしています。下の写真でわかるように:

ここに画像の説明を入力

getEmpty[9]戻ります:

[6,10]

しかし、私はそれを返す必要があります[6,8,9,10,11]

前の空のボックスとエッジを共有している限り、すべての空が欲しいからです。

この再帰を終了するにはどうすればよいですか?

現在、私は持っています

getEmpty(9)
        #my code here
        return empties;


empties = [6,10]

私はこれを追加しました:

for cell in empties:

        if(getEmpty(cell) not in empties):
            empties = empties+getEmpty(cell)

最後に、それは私に印刷される無限ループを与えます:

[6,10]
[9]

ノンストップ、これを修正するにはどうすればよいですか?

4

2 に答える 2

4

編集:申し訳ありませんが、あなたの質問は非常にあいまいでした。必要なのは、単純なグラフ走査アルゴリズムです。ここにそれを垣間見ることができます:

input : coordinate of a cell

function empty_cell_search(cell)
  for neighbor_cell in neighbors :
    if the_neighbor_cell is empty and is not already visited : 
      add the_neighbor_cell to set of visited cells
      union empty_cell_search(the_neighbor_cell) with current set of visited cells
  return the set of currently visited cells
于 2013-03-07T00:42:58.190 に答える
1

これを使用する必要があります(Python 3ですが、2についても同じ考えです)。

empty=[6,8,9,10,11]
start=9
done=[int(start)]
while True:
    complete=True
    for num0 in empty:
        for num1 in done:
            if (abs(num0-num1)==1 or abs(num1-num0)==3) and num0 not in done:
                complete=False
                done.append(num0)
    if complete=True:
         break
print(sorted(done))
于 2013-03-07T00:47:46.740 に答える