• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    python广度搜索解决八数码难题

    —— 八数码难题 ——

    1.题目描述

    八数码问题也称为九宫问题。在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同。棋盘上还有一个空格,与空格相邻的棋子可以移到空格中。要求解决的问题是:给出一个初始状态和一个目标状态,找出一种从初始状态转变成目标状态的移动棋子步数最少的移动步骤。

    代码

    使用算法:广度搜索算法

    python

    import numpy as np
    
    class 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, None
    
    if __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广度搜索八数码的资料请关注脚本之家其它相关文章!

    您可能感兴趣的文章:
    • python实现广度优先搜索过程解析
    • python广度优先搜索得到两点间最短路径
    • python 递归深度优先搜索与广度优先搜索算法模拟实现
    • python深度优先搜索和广度优先搜索
    • Python数据结构与算法之图的广度优先与深度优先搜索算法示例
    上一篇:PyQt5实现界面(页面)跳转的示例代码
    下一篇:PyQT5实现选项卡窗口、堆栈窗口、停靠窗口、子窗口
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    python广度搜索解决八数码难题 python,广度,搜索,解决,八,