-
Notifications
You must be signed in to change notification settings - Fork 0
/
重排九宫.py
200 lines (151 loc) · 5.76 KB
/
重排九宫.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# 重排九宫格
import numpy as np
import heapq
# 定义九宫格的初始状态和目标状态
grid = np.array([[2, 8, 3], [1, 0, 4], [7, 6, 5]])
target = np.array([[1, 2, 3], [8, 0, 4], [7, 6, 5]])
# 定义移动的四个方向:上、下、左、右
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
# 定义状态类
class State:
def __init__(self, grid, target, cost, parent=None):
self.grid = grid
self.target = target
self.cost = cost
self.parent = parent
def __lt__(self, other):
# 用于优先队列的比较函数
return self.cost < other.cost
def get_heuristic(self):
# 计算曼哈顿距离作为启发式函数
return np.sum(self.grid != self.target)
# A*算法函数
def astar_search(start, target):
open_list = []
closed_set = set()
heapq.heappush(open_list, start)
while open_list:
current_state = heapq.heappop(open_list)
if np.array_equal(current_state.grid, target):
# 找到目标状态,返回路径
path = []
while current_state:
path.append(current_state.grid)
current_state = current_state.parent
path.reverse()
return path
closed_set.add(tuple(map(tuple, current_state.grid)))
for direction in directions:
new_grid = current_state.grid.copy()
zero_pos = np.argwhere(new_grid == 0)[0]
new_pos = zero_pos + direction
if not (0 <= new_pos[0] < 3 and 0 <= new_pos[1] < 3):
continue
new_grid[zero_pos[0], zero_pos[1]] = new_grid[new_pos[0], new_pos[1]]
new_grid[new_pos[0], new_pos[1]] = 0
new_state = State(new_grid, target, current_state.cost + 1, current_state)
if tuple(map(tuple, new_state.grid)) in closed_set:
continue
heapq.heappush(open_list, new_state)
# 无法找到路径
return None
# 广度优先搜索算法
def bfs_search(start, target):
open_list = []
closed_set = set()
open_list.append(start)
while open_list:
current_state = open_list.pop(0)
if np.array_equal(current_state.grid, target):
# 找到目标状态,返回路径
path = []
while current_state:
path.append(current_state.grid)
current_state = current_state.parent
path.reverse()
return path
closed_set.add(tuple(map(tuple, current_state.grid)))
for direction in directions:
new_grid = current_state.grid.copy()
zero_pos = np.argwhere(new_grid == 0)[0]
new_pos = zero_pos + direction
if not (0 <= new_pos[0] < 3 and 0 <= new_pos[1] < 3):
continue
new_grid[zero_pos[0], zero_pos[1]] = new_grid[new_pos[0], new_pos[1]]
new_grid[new_pos[0], new_pos[1]] = 0
new_state = State(new_grid, target, current_state.cost + 1, current_state)
if tuple(map(tuple, new_state.grid)) in closed_set:
continue
open_list.append(new_state)
# 无法找到路径
return None
# 深度优先搜索算法
def dfs_search(start, target):
open_list = []
closed_set = set()
open_list.append(start)
while open_list:
current_state = open_list.pop(0)
if np.array_equal(current_state.grid, target):
# 找到目标状态,返回路径
path = []
while current_state:
path.append(current_state.grid)
current_state = current_state.parent
path.reverse()
return path
closed_set.add(tuple(map(tuple, current_state.grid)))
for direction in directions:
new_grid = current_state.grid.copy()
zero_pos = np.argwhere(new_grid == 0)[0]
new_pos = zero_pos + direction
if not (0 <= new_pos[0] < 3 and 0 <= new_pos[1] < 3):
continue
new_grid[zero_pos[0], zero_pos[1]] = new_grid[new_pos[0], new_pos[1]]
new_grid[new_pos[0], new_pos[1]] = 0
new_state = State(new_grid, target, current_state.cost + 1, current_state)
if tuple(map(tuple, new_state.grid)) in closed_set:
continue
open_list.insert(0, new_state)
# 无法找到路径
return None
if __name__ == '__main__':
# 创建初始状态
start_state = State(grid, target, 0)
# 运行A*算法
import time
start_time = time.time()
path = astar_search(start_state, target)
end_time = time.time()
print("运行时间:", end_time - start_time)
print("移动步数:", len(path) - 1)
if path:
print("找到路径:")
for state in path:
print(state)
else:
print("无法找到路径")
# 运行广度优先搜索算法
start_time = time.time()
path = bfs_search(start_state, target)
end_time = time.time()
print("运行时间:", end_time - start_time)
print("移动步数:", len(path) - 1)
if path:
print("找到路径:")
for state in path:
print(state)
else:
print("无法找到路径")
# 运行深度优先搜索算法
start_time = time.time()
path = dfs_search(start_state, target)
end_time = time.time()
print("运行时间:", end_time - start_time)
print("移动步数:", len(path) - 1)
if path:
print("找到路径:")
for state in path:
print(state)
else:
print("无法找到路径")