-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathids.py
40 lines (32 loc) · 895 Bytes
/
ids.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
pynpuzzle - Solve n-puzzle with Python
Iterative deepening depth-first search algorithm
Version : 1.0.0
Author : Hamidreza Mahdavipanah
Repository: http://github.com/mahdavipanah/pynpuzzle
License : MIT License
"""
from .util.tree_search import Node
def search(state, goal_state):
"""Iterative deepening depth-first"""
depth = 0
def dls(node):
if node.is_goal(goal_state):
return node
if node.depth < depth:
node.expand()
for child in node.children:
result = dls(child)
if result:
return result
return None
answer = None
while not answer:
answer = dls(Node(state))
depth += 1
output = []
output.append(answer.state)
for parent in answer.parents():
output.append(parent.state)
output.reverse()
return output