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

 更新时间:2021年04月07日 14:18:20   作者:胡乱huluan  
这篇文章主要介绍了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广度搜索八数码的资料请关注脚本之家其它相关文章!

相关文章

  • 最新pycharm安装教程

    最新pycharm安装教程

    这篇文章主要介绍了最新pycharm安装教程,需要的朋友可以参考下
    2020-11-11
  • 基于Pandas读取csv文件Error的总结

    基于Pandas读取csv文件Error的总结

    今天小编就为大家分享一篇基于Pandas读取csv文件Error的总结,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • 深入解析python返回函数和匿名函数

    深入解析python返回函数和匿名函数

    这篇文章主要介绍了python返回函数和匿名函数的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 解决PyCharm 中写 Turtle代码没提示以及标黄的问题

    解决PyCharm 中写 Turtle代码没提示以及标黄的问题

    这篇文章主要介绍了解决PyCharm 中写 Turtle代码没提示以及标黄的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python检测网络延迟的代码

    Python检测网络延迟的代码

    这篇文章主要介绍了Python检测网络延迟的代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • python中的__init__ 、__new__、__call__小结

    python中的__init__ 、__new__、__call__小结

    这篇文章主要介绍了python中的__init__ 、__new__、__call__小结,需要的朋友可以参考下
    2014-04-04
  • Python生成随机数的方法

    Python生成随机数的方法

    这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下
    2014-01-01
  • python安装以及IDE的配置教程

    python安装以及IDE的配置教程

    Python在Linux、windows、Mac os等操作系统下都有相应的版本,不管在什么操作系统下,它都能够正常工作。除非使用平台相关功能,或特定平台的程序库,否则可以跨平台使用。今天我们主要来探讨下windows系统下的安装与配置
    2015-04-04
  • Python利用pangu模块实现文本格式化小工具

    Python利用pangu模块实现文本格式化小工具

    其实使用pangu做文本格式标准化的业务代码在之前就实现了,主要能够将中文文本文档中的文字、标点符号等进行标准化。但是为了方便起来我们这里使用了Qt5将其做成了一个可以操作的页面应用,需要的可以了解一下
    2022-10-10
  • 使用python装饰器验证配置文件示例

    使用python装饰器验证配置文件示例

    项目中用到了一个WriteData的函数保存用户填写的配置,为了实现验证用户输入的需求,在不影响接口的使用的前提下,采用了python的装饰器实现,代码片段演示了如何验证WriteData函数的输入参数
    2014-02-02

最新评论