八数码可视化求解及代码
时间: 2023-09-24 16:11:48 浏览: 113
八数码问题可以使用搜索算法求解,比较常用的有A*算法、IDA*算法、BFS等。这里提供一个使用BFS算法求解八数码问题的Python代码实现,包括八数码状态的表示、判断是否可解、BFS求解等功能。
```python
# 使用BFS算法求解八数码问题
from queue import Queue
# 定义八数码状态类
class State:
def __init__(self, s, step=0, pre=None):
self.s = s # 八数码状态
self.step = step # 步数
self.pre = pre # 前一状态
# 判断当前状态是否为目标状态
def is_target(self, target):
return self.s == target
# 获取当前状态的所有后继状态
def get_next_states(self):
next_states = []
zero_index = self.s.index(0) # 获取空格的位置
if zero_index == 0:
next_states.append(State(self.swap(self.s, zero_index, 1), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 3), self.step + 1, self))
elif zero_index == 1:
next_states.append(State(self.swap(self.s, zero_index, 0), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 2), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 4), self.step + 1, self))
elif zero_index == 2:
next_states.append(State(self.swap(self.s, zero_index, 1), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 5), self.step + 1, self))
elif zero_index == 3:
next_states.append(State(self.swap(self.s, zero_index, 0), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 4), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 6), self.step + 1, self))
elif zero_index == 4:
next_states.append(State(self.swap(self.s, zero_index, 1), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 3), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 5), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 7), self.step + 1, self))
elif zero_index == 5:
next_states.append(State(self.swap(self.s, zero_index, 2), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 4), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 8), self.step + 1, self))
elif zero_index == 6:
next_states.append(State(self.swap(self.s, zero_index, 3), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 7), self.step + 1, self))
elif zero_index == 7:
next_states.append(State(self.swap(self.s, zero_index, 4), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 6), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 8), self.step + 1, self))
elif zero_index == 8:
next_states.append(State(self.swap(self.s, zero_index, 5), self.step + 1, self))
next_states.append(State(self.swap(self.s, zero_index, 7), self.step + 1, self))
return next_states
# 交换列表中的两个元素
def swap(self, s, i, j):
s = list(s)
s[i], s[j] = s[j], s[i]
return tuple(s)
# 判断八数码是否可解
def is_solvable(s):
inv = 0
for i in range(8):
for j in range(i + 1, 9):
if s[j] and s[i] and s[i] > s[j]:
inv += 1
return inv % 2 == 0
# BFS求解八数码问题
def bfs(start, target):
q = Queue()
visited = set()
q.put(start)
visited.add(start.s)
while not q.empty():
cur = q.get()
if cur.is_target(target):
return cur
for next_state in cur.get_next_states():
if next_state.s not in visited:
q.put(next_state)
visited.add(next_state.s)
return None
# 测试
if __name__ == '__main__':
start = State((2, 8, 3, 1, 6, 4, 7, 0, 5)) # 初始状态
target = (1, 2, 3, 8, 0, 4, 7, 6, 5) # 目标状态
if not is_solvable(start.s):
print("无解")
else:
res = bfs(start, target)
if res:
# 打印求解路径
path = []
while res:
path.append(res.s)
res = res.pre
path.reverse()
for p in path:
print(p[0:3])
print(p[3:6])
print(p[6:9])
print()
else:
print("无解")
```
这段代码实现了八数码状态的表示、判断是否可解、BFS求解等功能,可以通过调用`bfs`函数来求解八数码问题。当八数码问题无解时,程序会输出“无解”;当八数码问题有解时,程序会输出求解路径。
阅读全文