最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python廣度搜索解決八數(shù)碼難題

 更新時間:2021年04月07日 14:18:20   作者:胡亂huluan  
這篇文章主要介紹了python廣度搜索解決八數(shù)碼難題。想了解算法和數(shù)據(jù)結(jié)構(gòu)的同學(xué),一定要看一下

—— 八數(shù)碼難題 ——

1.題目描述

八數(shù)碼問題也稱為九宮問題。在3×3的棋盤,擺有八個棋子,每個棋子上標(biāo)有1至8的某一數(shù)字,不同棋子上標(biāo)的數(shù)字不相同。棋盤上還有一個空格,與空格相鄰的棋子可以移到空格中。要求解決的問題是:給出一個初始狀態(tài)和一個目標(biāo)狀態(tài),找出一種從初始狀態(tài)轉(zhuǎn)變成目標(biāo)狀態(tài)的移動棋子步數(shù)最少的移動步驟。

代碼

使用算法:廣度搜索算法

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廣度搜索解決八數(shù)碼難題的詳細(xì)內(nèi)容,更多關(guān)于python廣度搜索八數(shù)碼的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 最新pycharm安裝教程

    最新pycharm安裝教程

    這篇文章主要介紹了最新pycharm安裝教程,需要的朋友可以參考下
    2020-11-11
  • 基于Pandas讀取csv文件Error的總結(jié)

    基于Pandas讀取csv文件Error的總結(jié)

    今天小編就為大家分享一篇基于Pandas讀取csv文件Error的總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • 深入解析python返回函數(shù)和匿名函數(shù)

    深入解析python返回函數(shù)和匿名函數(shù)

    這篇文章主要介紹了python返回函數(shù)和匿名函數(shù)的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 解決PyCharm 中寫 Turtle代碼沒提示以及標(biāo)黃的問題

    解決PyCharm 中寫 Turtle代碼沒提示以及標(biāo)黃的問題

    這篇文章主要介紹了解決PyCharm 中寫 Turtle代碼沒提示以及標(biāo)黃的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python檢測網(wǎng)絡(luò)延遲的代碼

    Python檢測網(wǎng)絡(luò)延遲的代碼

    這篇文章主要介紹了Python檢測網(wǎng)絡(luò)延遲的代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • python中的__init__ 、__new__、__call__小結(jié)

    python中的__init__ 、__new__、__call__小結(jié)

    這篇文章主要介紹了python中的__init__ 、__new__、__call__小結(jié),需要的朋友可以參考下
    2014-04-04
  • Python生成隨機(jī)數(shù)的方法

    Python生成隨機(jī)數(shù)的方法

    這篇文章主要介紹了Python生成隨機(jī)數(shù)的方法,有需要的朋友可以參考一下
    2014-01-01
  • python安裝以及IDE的配置教程

    python安裝以及IDE的配置教程

    Python在Linux、windows、Mac os等操作系統(tǒng)下都有相應(yīng)的版本,不管在什么操作系統(tǒng)下,它都能夠正常工作。除非使用平臺相關(guān)功能,或特定平臺的程序庫,否則可以跨平臺使用。今天我們主要來探討下windows系統(tǒng)下的安裝與配置
    2015-04-04
  • Python利用pangu模塊實現(xiàn)文本格式化小工具

    Python利用pangu模塊實現(xiàn)文本格式化小工具

    其實使用pangu做文本格式標(biāo)準(zhǔn)化的業(yè)務(wù)代碼在之前就實現(xiàn)了,主要能夠?qū)⒅形奈谋疚臋n中的文字、標(biāo)點符號等進(jìn)行標(biāo)準(zhǔn)化。但是為了方便起來我們這里使用了Qt5將其做成了一個可以操作的頁面應(yīng)用,需要的可以了解一下
    2022-10-10
  • 使用python裝飾器驗證配置文件示例

    使用python裝飾器驗證配置文件示例

    項目中用到了一個WriteData的函數(shù)保存用戶填寫的配置,為了實現(xiàn)驗證用戶輸入的需求,在不影響接口的使用的前提下,采用了python的裝飾器實現(xiàn),代碼片段演示了如何驗證WriteData函數(shù)的輸入?yún)?shù)
    2014-02-02

最新評論

乐昌市| 增城市| 广昌县| 舞钢市| 拜泉县| 上犹县| 安阳县| 神木县| 宜州市| 噶尔县| 方山县| 铜陵市| 古浪县| 丰都县| 城口县| 成都市| 名山县| 遵义市| 曲松县| 彭州市| 泰宁县| 葵青区| 岑巩县| 鲁甸县| 象州县| 清徐县| 谢通门县| 额尔古纳市| 枝江市| 宁都县| 秦皇岛市| 噶尔县| 中阳县| 城口县| 沁源县| 临邑县| 吐鲁番市| 岑巩县| 宽城| 高雄市| 清徐县|