av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:4日期:2022-06-23 11:07:58
—— 八數(shù)碼難題 ——1.題目描述

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

代碼

使用算法:廣度搜索算法

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ù)碼難題的詳細內(nèi)容,更多關(guān)于python廣度搜索八數(shù)碼的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 看片天堂| 黄网站涩免费蜜桃网站 | 久久久一区二区三区四区 | 日本超碰 | 亚洲午夜在线 | 久久久.com| 欧美不卡网站 | 亚洲一区二区精品视频 | 成人久久18免费网站麻豆 | 新疆少妇videos高潮 | 日韩精品一区二区三区四区 | 91国产在线视频在线 | 精品免费视频一区二区 | 久久久精品网 | 免费看a | 久久精品综合 | 日韩成人在线播放 | 久久精品国产亚洲 | 成人在线精品视频 | 成人av免费在线观看 | 久久久久综合 | 日韩午夜 | 在线观看黄色电影 | 国产视频不卡一区 | 成人在线免费 | 99久久久久久久 | 国产偷录叫床高潮录音 | 久久99视频| 黄色大片在线免费观看 | 欧美激情啪啪 | 国产三级在线观看播放 | 免费观看一级特黄欧美大片 | 欧洲一级毛片 | 91爱啪啪 | 99精品久久久久 | 男人的天堂亚洲 | 国产探花在线精品一区二区 | 午夜视频在线播放 | hdfreexxxx中国妞| 亚洲欧美一区在线 | 最新国产视频 |