欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 利用Python實(shí)現(xiàn)批量加密Excel文件

    利用Python實(shí)現(xiàn)批量加密Excel文件

    在日常工作中,我們經(jīng)常需要處理大量的Excel文件,為了保護(hù)敏感數(shù)據(jù)的安全性,我們可能需要對這些文件進(jìn)行加密,本文主要介紹了如何使用Python實(shí)現(xiàn)批量加密Excel文件,需要的可以參考下
    2023-11-11
  • 對pandas replace函數(shù)的使用方法小結(jié)

    對pandas replace函數(shù)的使用方法小結(jié)

    今天小編就為大家分享一篇對pandas replace函數(shù)的使用方法小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 詳解Python如何獲取視頻文件的大小和時長

    詳解Python如何獲取視頻文件的大小和時長

    這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)獲取視頻文件的大小和時長,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-03-03
  • 詳解【python】str與json類型轉(zhuǎn)換

    詳解【python】str與json類型轉(zhuǎn)換

    這篇文章主要介紹了【python】str與json類型轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python使用scipy.fft進(jìn)行大學(xué)經(jīng)典的傅立葉變換

    Python使用scipy.fft進(jìn)行大學(xué)經(jīng)典的傅立葉變換

    傅里葉變換是在高數(shù)是一個很重要的知識點(diǎn),本文將介紹Python使用scipy.fft進(jìn)行大學(xué)經(jīng)典的傅立葉變換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • 使用Python進(jìn)行AES加密和解密的示例代碼

    使用Python進(jìn)行AES加密和解密的示例代碼

    這篇文章主要介紹了使用Python進(jìn)行AES加密和解密的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • django配置DJANGO_SETTINGS_MODULE的實(shí)現(xiàn)

    django配置DJANGO_SETTINGS_MODULE的實(shí)現(xiàn)

    本文主要介紹了django配置DJANGO_SETTINGS_MODULE,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • Pycharm使用Conda激活環(huán)境失敗的問題解決

    Pycharm使用Conda激活環(huán)境失敗的問題解決

    本文主要介紹了Pycharm使用Conda激活環(huán)境失敗的問題解決,文中主要介紹了兩種問題的解決,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī)

    Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī)

    這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Python堆棧的具體使用

    Python堆棧的具體使用

    本文主要介紹了Python堆棧的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11

最新評論