python廣度搜索解決八數(shù)碼難題
八數(shù)碼問(wèn)題也稱為九宮問(wèn)題。在3×3的棋盤,擺有八個(gè)棋子,每個(gè)棋子上標(biāo)有1至8的某一數(shù)字,不同棋子上標(biāo)的數(shù)字不相同。棋盤上還有一個(gè)空格,與空格相鄰的棋子可以移到空格中。要求解決的問(wèn)題是:給出一個(gè)初始狀態(tài)和一個(gè)目標(biāo)狀態(tài),找出一種從初始狀態(tài)轉(zhuǎn)變成目標(biāo)狀態(tài)的移動(dòng)棋子步數(shù)最少的移動(dòng)步驟。
代碼使用算法:廣度搜索算法
python
import numpy as npclass 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, Noneif __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ù)碼的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令2. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. 使用Hangfire+.NET 6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)4. 如何在jsp界面中插入圖片5. phpstudy apache開啟ssi使用詳解6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器7. jsp文件下載功能實(shí)現(xiàn)代碼8. 詳解瀏覽器的緩存機(jī)制9. 爬取今日頭條Ajax請(qǐng)求10. xml中的空格之完全解說(shuō)
