python广度搜索解决八数码难题
作者:胡乱huluan 发布时间:2023-01-26 18:12:43
标签: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)
来源:https://blog.csdn.net/qq_44867435/article/details/115445456


猜你喜欢
- 简介本文主要简述如何通过sklearn模块来进行预测和学习,最后再以图表这种更加直观的方式展现出来数据集学习数据预测数据数据处理数据分离因为
- 大家好,今天分享一个非常有趣的 Python 教程,如何美化一个 matplotlib 折线图,喜欢记得收藏、关注、点赞。1. 导入包imp
- Python2.x使用过程中,中文乱码解决最耳熟能详的方法就是在代码前加上#-*- coding:utf-8 –*-那么为什么需要这么做呢?
- 安装laravel框架命令行cd进入指定目录下,执行composer create-project --prefer-dist larave
- 一、逻辑数据库和表的设计数据库的逻辑设计、包括表与表之间的关系是优化关系型数据库性能的核心。一个好的逻辑数据库设计可以为优化数据库和应用程序
- 用法: 按住鼠标左键拖拽一个框后释放洗洗睡了<!DOCTYPE html public "-//W3C//DTD XHTML
- 前言:工作中遇到以下小问题,解决方法如下,可能比较暴力,暂时留档,再进行优化。要求:将列表中json的 ‘id&
- 看例子: 数 据表 collect ( id, title ,info ,vtype) 就这4个字段,其中 title 用定长,info 用
- 1 re.search() 的作用:re.search会匹配整个字符串,并返回第一个成功的匹配。如果匹配失败,则返回None 从源
- 在现在的项目里,不管是电商项目还是别的项目,在管理端都会有导出的功能,比方说订单表导出,用户表导出,业绩表导出。这些都需要提前生成excel
- /r的用法与end=""用法 \r 表示将光标的位置回退到本行的开头位置end="" 意思
- 前言本文结合一个具体的无向图来对最简单的一种GNN进行推导。本文第一部分是数据介绍,第二部分为推导过程中需要用的变量的定义,第三部分是GNN
- 切片的声明切片可以看成是数组的引用(实际上切片的底层数据结构确实是数组)。在 Go 中,每个数组的大小是固定的,不能随意
- 对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关
- 最近在使用linux上进行本地登录时,发现既然无法正常登录 , 报如下错误信息:[root@xxxx ~]# mysql -h localh
- 统一捕获接口报错弹窗提示报错重定向基础鉴权表单序列化实现的功能统一捕获接口报错 : 用的axios内置的 * 弹窗提示: 引入 E
- Python开发最牛逼的IDE——pycharm(其实其它的工具,例如eclipse也可以写,只不过比较麻烦,需要安装很多的插件,所以说py
- 前言相信大家在日常使用mysql,可能会遇到需要同时更新两张表时,我会采用在同一个事务中使用2句sql语句分别进行更新。其实,这种需要发送2
- 道友问我的一个问题,之前确实没遇见过,在此记录一下。问题描述在某网站主页提取url进行迭代,爬虫请求主页时没有问题,返回正常,但是在访问在主
- pytorch 预训练模型读取修改相关参数的填坑修改部分层,仍然调用之前的模型参数。resnet = resnet50(pretrained