首页 > python教程

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

时间:2021-05-14 python教程 查看: 853

—— 八数码难题 ——

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博客其它相关文章!

展开全文
上一篇:pandas DataFrame 赋值的注意事项说明(index)
下一篇:解决python存数据库速度太慢的问题
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下