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

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

熱門標(biāo)簽:語音系統(tǒng) 呼叫中心市場需求 硅谷的囚徒呼叫中心 百度AI接口 電話運營中心 Win7旗艦版 客戶服務(wù) 企業(yè)做大做強

—— 八數(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)文章!

您可能感興趣的文章:
  • python實現(xiàn)廣度優(yōu)先搜索過程解析
  • python廣度優(yōu)先搜索得到兩點間最短路徑
  • python 遞歸深度優(yōu)先搜索與廣度優(yōu)先搜索算法模擬實現(xiàn)
  • python深度優(yōu)先搜索和廣度優(yōu)先搜索
  • Python數(shù)據(jù)結(jié)構(gòu)與算法之圖的廣度優(yōu)先與深度優(yōu)先搜索算法示例

標(biāo)簽:崇左 山西 海南 喀什 安康 濟南 長沙 山西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python廣度搜索解決八數(shù)碼難題》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266