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

作者:胡乱huluan 时间:2023-01-26 18:12:43 

—— 八数码难题 ——

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)

来源:https://blog.csdn.net/qq_44867435/article/details/115445456

标签:python,八数码,广度搜索
0
投稿

猜你喜欢

  • asp如何限制重复订阅邮件或重复投票?

    2010-06-09 18:48:00
  • VS2013设置护眼背景颜色

    2023-06-28 12:59:02
  • Python数据分析之NumPy常用函数使用详解

    2021-09-29 06:17:33
  • 创建SparkSession和sparkSQL的详细过程

    2023-02-13 14:40:40
  • Python 读取某个目录下所有的文件实例

    2022-08-27 20:04:13
  • Python在for循环中更改list值的方法【推荐】

    2023-03-05 07:14:28
  • python 多线程应用介绍

    2023-07-13 08:04:04
  • python scrapy重复执行实现代码详解

    2023-01-15 17:27:37
  • 获得MySQL改变字符集的方案

    2010-08-31 14:53:00
  • asp之日期和时间函数示例

    2008-04-13 06:50:00
  • 如何设计注册激活邮件

    2010-01-12 13:14:00
  • 将有安全问题的SQL过程删除,比较全面

    2007-08-06 14:46:00
  • 使用Perl语言去存取mSQL和MySQL数据库的内容

    2009-10-23 09:11:00
  • sql server海量数据库的查询优化及分页算法方案

    2010-07-02 21:17:00
  • 菜鸟课堂:MySQL权限的详细解答

    2009-09-03 11:43:00
  • Python可变和不可变、类的私有属性实例分析

    2023-05-27 14:34:08
  • python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法

    2021-12-16 16:24:37
  • 详解vue 模拟后台数据(加载本地json文件)调试

    2023-07-16 18:25:57
  • 几个ASP字符串处理函数

    2008-04-23 12:55:00
  • ASP.Net MVC 布局页、模板页使用方法详细介绍

    2023-06-28 19:24:31
  • asp之家 网络编程 m.aspxhome.com