puzzle.extensions(self)
kodunun önceden tanımlanmış olduğunu varsayalım ve çözmenin mümkün olup olmadığını belirlemek için bulmacanın kullanılabilir çözümlerini içeren bir liste döndürecektir. Ayrıca puzzle.is_solved(self)
tanımlanmıştır ve bu çözümün çözülüp çözülmeyeceğini belirleyecektir. İşte yazmam gereken kod, bazı yanlış işler de yapıyorum.Derinlik ilk arama yapmak için Çöz:
def depth_first_solve(puzzle):
"""
Return a path from PuzzleNode(puzzle) to a PuzzleNode containing
a solution, with each child containing an extension of the puzzle
in its parent. Return None if this is not possible.
@type puzzle: Puzzle
@rtype: PuzzleNode
"""
stack = [puzzle]
while stack:
k = stack.pop()
for puzzle1 in puzzle.extensions():
if not puzzle1.is_solved():
stack+=[k,puzzle1]
if puzzle1.is_solved():
p = stack.pop()
end_node = PuzzleNode(k,None, p)
k = stack.pop()
last_node = PuzzleNode(p,end_node,k)
while stack:
k = p
p = stack.pop()
cur_node = PuzzleNode(k, last_node, p)
last_node = cur_node
return cur_node
def __init__(self, puzzle=None, children=None, parent=None):
"""
Create a new puzzle node self with configuration puzzle.
@type self: PuzzleNode
@type puzzle: Puzzle | None
@type children: list[PuzzleNode]
@type parent: PuzzleNode | None
@rtype: None
"""
self.puzzle, self.parent = puzzle, parent
if children is None:
self.children = []
else:
self.children = children[:]
Eh, bulmaca bu modül çalıştırmak ve her zaman sonuçlarını beklerken ve hiçbir şey olmuyor, bu yüzden kimse yanlış anladım nerede olduğunu bana söyleyebilir?