婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python廣度搜索解決八數碼難題

python廣度搜索解決八數碼難題

熱門標簽:400電話申請服務商選什么 原裝電話機器人 工廠智能電話機器人 清遠360地圖標注方法 在哪里辦理400電話號碼 千陽自動外呼系統 西藏智能外呼系統五星服務 平頂山外呼系統免費 江蘇客服外呼系統廠家

—— 八數碼難題 ——

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實現廣度優先搜索過程解析
  • python廣度優先搜索得到兩點間最短路徑
  • python 遞歸深度優先搜索與廣度優先搜索算法模擬實現
  • python深度優先搜索和廣度優先搜索
  • Python數據結構與算法之圖的廣度優先與深度優先搜索算法示例

標簽:天水 隨州 股票 安慶 錦州 日照 白城 西安

巨人網絡通訊聲明:本文標題《python廣度搜索解決八數碼難題》,本文關鍵詞  python,廣度,搜索,解決,八,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python廣度搜索解決八數碼難題》相關的同類信息!
  • 本頁收集關于python廣度搜索解決八數碼難題的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 波密县| 武夷山市| 舟曲县| 慈溪市| 象山县| 镇坪县| 新竹县| 溧阳市| 西贡区| 沙坪坝区| 芦山县| 广灵县| 永川市| 阿克苏市| 武乡县| 道孚县| 镇远县| 根河市| 县级市| 天峨县| 刚察县| 遵义县| 南汇区| 永年县| 会理县| 乌兰察布市| 莱西市| 宁国市| 扎鲁特旗| 灵台县| 高邮市| 独山县| 多伦县| 年辖:市辖区| 马公市| 昌吉市| 贵南县| 桐梓县| 南康市| 揭东县| 太和县|