时间:2021-05-23
八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始状态转变成目标状态的移动棋子步数最少的移动步骤。
使用算法:广度搜索算法
python
import numpy as npclass State: def __init__(self, state, directionFlag=None, parent=None): self.state = state self.direction = ['up', 'down', 'right', 'left'] if directionFlag: self.direction.remove(directionFlag) self.parent = parent self.symbol = ' ' def getDirection(self): return self.direction def showInfo(self): for i in range(3): for j in range(3): print(self.state[i, j], end=' ') print("\n") print('->\n') return def getEmptyPos(self): postion = np.where(self.state == self.symbol) return postion def generateSubStates(self): if not self.direction: return [] subStates = [] boarder = len(self.state) - 1 row, col = self.getEmptyPos() if 'left' in self.direction and col > 0: s = self.state.copy() temp = s.copy() s[row, col] = s[row, col-1] s[row, col-1] = temp[row, col] news = State(s, directionFlag='right', parent=self) subStates.append(news) if 'up' in self.direction and row > 0: s = self.state.copy() temp = s.copy() s[row, col] = s[row-1, col] s[row-1, col] = temp[row, col] news = State(s, directionFlag='down', parent=self) subStates.append(news) if 'down' in self.direction and row < boarder: s = self.state.copy() temp = s.copy() s[row, col] = s[row+1, col] s[row+1, col] = temp[row, col] news = State(s, directionFlag='up', parent=self) subStates.append(news) if self.direction.count('right') and col < boarder: s = self.state.copy() temp = s.copy() s[row, col] = s[row, col+1] s[row, col+1] = temp[row, col] news = State(s, directionFlag='left', parent=self) subStates.append(news) return subStates def solve(self): openTable = [] closeTable = [] openTable.append(self) steps = 1 while len(openTable) > 0: n = openTable.pop(0) closeTable.append(n) subStates = n.generateSubStates() path = [] for s in subStates: if (s.state == s.answer).all(): while s.parent and s.parent != originState: path.append(s.parent) s = s.parent path.reverse() return path, steps+1 openTable.extend(subStates) steps += 1 else: return None, Noneif __name__ == '__main__': symbolOfEmpty = ' ' State.symbol = symbolOfEmpty originState = State(np.array([[2, 8, 3], [1, 6 , 4], [7, symbolOfEmpty, 5]])) State.answer = np.array([[1, 2, 3], [8, State.symbol, 4], [7, 6, 5]]) s1 = State(state=originState.state) path, steps = s1.solve() if path: for node in path: node.showInfo() print(State.answer) print("Total steps is %d" % steps)以上就是python广度搜索解决八数码难题的详细内容,更多关于python广度搜索八数码的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
算法介绍迪科斯彻算法使用了广度优先搜索解决赋权有向图或者无向图的单源最短路径问题,算法最终得到一个最短路径树。该算法常用于路由算法或者作为其他图算法的一个子模块
本文实例讲述了Python数据结构与算法之图的广度优先与深度优先搜索算法。分享给大家供大家参考,具体如下:根据维基百科的伪代码实现:广度优先BFS:使用队列,集
本文实例讲述了PHP实现广度优先搜索算法。分享给大家供大家参考,具体如下:广度优先搜索的算法思想Breadth-FirstTraversal广度优先遍历是连通图
这种情况在Access下可以通过,但SQL则不行,在百度搜索解决方法,发现N多人出现同样问题却找不到解决办法:复制代码代码如下:setrs=server.Cre
在使用layui的多图上传时发现没有删除功能在网上搜索解决办法时有的感觉太复杂有的不符合自己所需要的所以就自己动手下面附上代码HTML:多图片上传预览图:CSS