資源簡介
用python實現深度,廣度,三種啟發式搜索解決八數碼問題。附有圖形化界面和可執行文件。
并且有詳細的實現思路。
代碼片段和文件信息
#------------八數碼問題
import?tkinter?as?tk
from??tkinter?import?ttk??#導入內部包
import?random
import?problem1
import?problem2
import?problem3
import?problem4
import?problem5
?
win=tk.Tk()
win.title(‘八數碼問題‘)
tipslb=tk.Label(win?text=“注:深度搜索中搜索最大深度設為8為了節省運行時間,?\
\n廣度搜索被設置為至多遍歷1000個節點,點擊下方兩個按鈕選擇手動或隨機產生?\
\nNone代表算法結束時仍還沒找到結果“bg=‘#B5BBB5‘)
tipslb.pack()
label1=tk.Label(wintext=“輸入初始狀態:“)
label1.pack()
#輸入框
e_text=tk.Entry(winshow=Nonefont=(‘Arial‘?14))
e_text.pack(padx=2pady=10)
e_text.insert(0‘283164705‘)
label2=tk.Label(wintext=“輸入目標狀態:“)
label2.pack()
e_text2=tk.Entry(winshow=Nonefont=(‘Arial‘?14))
e_text2.pack(padx=20pady=10)
e_text2.insert(0‘123804765‘)
#隨機生成初始數據
def?random_excute():
????str_v1=‘‘.join(random.sample(‘012345678‘9))
????str_v2=‘‘.join(random.sample(‘012345678‘9))
????textshow.insert(‘end‘‘隨機生成的數據:\n‘)
????textshow.insert(‘end‘str_v1)
????textshow.insert(‘end‘‘---->\n‘)
????textshow.insert(‘end‘str_v2+‘\n‘)
????insert_v(str_v1str_v2)
#獲取輸入數據???
def?get_input_values():
????str1=e_text.get()
????str2=e_text2.get()
????insert_v(str1str2)
#插入數據
def?insert_v(str1str2):
????r1ar1b=problem1.DFS_main(str1str2)
????r2ar2b=problem2.BFS(str1str2)
????r3ar3b=problem3.main(str1str2)
????r4ar4b=problem4.heuristic_search(str1str2)
????r5ar5b=problem5.HS5.run(str1str2)
????num=[‘擴展節點數‘r1ar2ar3ar4ar5a]??????
????tree.insert(““0values=num)?#插入數據
????
????num2=[‘生成節點數‘r1br2br3br4br5b]???????
????tree.insert(““1values=num2)?#插入數據
????
????tree.insert(‘‘0)#插入空行
????
????
#顯示數據的文本框??
textshow?=?tk.Text(win?width=20height=3)
tree=ttk.Treeview(winshow=“headings“)#表格
tree[“columns“]=(“方法““深搜““寬搜““h1““h2““h3“)
tree.column(“方法“width=100)???#表示列不顯示
for?col?in?tree[“columns“][1:]:
????tree.column(colwidth=70)
?
tree.heading(“方法“text=“方法->“)??#顯示表頭
tree.heading(“深搜“text=“深度優先“)
tree.heading(“寬搜“text=“寬度優先“)
tree.heading(“h1“text=“h1(n)“)
tree.heading(“h2“text=‘h2(n)‘)
tree.heading(‘h3‘text=‘h3(n)‘)????
????
excute=tk.Button(wintext=‘根據輸入框數據計算‘command=get_input_valueswidth=20height=2)
rand_ex=tk.Button(wintext=‘隨機生成數據計算‘command=random_excutewidth=20height=2)
excute.pack()
rand_ex.pack()
textshow.pack()
tree.pack()
win.mainloop()
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-05-10?21:55??八數碼問題\
?????文件????35279182??2020-05-05?15:14??八數碼問題\可執行程序.exe
?????文件????????9176??2020-05-10?21:55??八數碼問題\思想.txt
?????目錄???????????0??2020-05-05?14:20??八數碼問題\源程序\
?????目錄???????????0??2020-05-05?14:54??八數碼問題\源程序\.idea\
?????目錄???????????0??2020-05-05?15:36??八數碼問題\源程序\.idea\inspectionProfiles\
?????文件?????????174??2020-04-25?13:01??八數碼問題\源程序\.idea\inspectionProfiles\profiles_settings.xm
?????文件?????????199??2020-04-25?13:01??八數碼問題\源程序\.idea\misc.xm
?????文件?????????273??2020-04-25?13:01??八數碼問題\源程序\.idea\modules.xm
?????文件?????????452??2020-04-25?13:01??八數碼問題\源程序\.idea\resolve.iml
?????文件????????5016??2020-05-05?14:54??八數碼問題\源程序\.idea\workspace.xm
?????目錄???????????0??2020-05-05?15:37??八數碼問題\源程序\__pycache__\
?????文件????????3894??2020-04-25?13:01??八數碼問題\源程序\__pycache__\problem1.cpython-36.pyc
?????文件????????3932??2020-05-05?15:37??八數碼問題\源程序\__pycache__\problem1.cpython-37.pyc
?????文件????????3922??2020-04-25?12:58??八數碼問題\源程序\__pycache__\problem1.cpython-38.pyc
?????文件????????3354??2020-04-25?13:01??八數碼問題\源程序\__pycache__\problem2.cpython-36.pyc
?????文件????????3387??2020-05-05?15:37??八數碼問題\源程序\__pycache__\problem2.cpython-37.pyc
?????文件????????3377??2020-04-25?12:58??八數碼問題\源程序\__pycache__\problem2.cpython-38.pyc
?????文件????????3106??2020-05-05?14:20??八數碼問題\源程序\__pycache__\problem3.cpython-36.pyc
?????文件????????3134??2020-05-05?15:37??八數碼問題\源程序\__pycache__\problem3.cpython-37.pyc
?????文件????????3112??2020-04-25?12:58??八數碼問題\源程序\__pycache__\problem3.cpython-38.pyc
?????文件????????2542??2020-04-25?13:01??八數碼問題\源程序\__pycache__\problem4.cpython-36.pyc
?????文件????????2573??2020-05-05?15:37??八數碼問題\源程序\__pycache__\problem4.cpython-37.pyc
?????文件????????2543??2020-04-25?12:58??八數碼問題\源程序\__pycache__\problem4.cpython-38.pyc
?????文件????????6102??2020-04-25?13:01??八數碼問題\源程序\__pycache__\problem5.cpython-36.pyc
?????文件????????6006??2020-05-05?15:37??八數碼問題\源程序\__pycache__\problem5.cpython-37.pyc
?????文件????????5997??2020-04-25?12:58??八數碼問題\源程序\__pycache__\problem5.cpython-38.pyc
?????文件????????2756??2020-04-25?12:58??八數碼問題\源程序\mainGUI.py
?????文件????????5209??2020-04-25?12:58??八數碼問題\源程序\problem1.py
?????文件????????5168??2020-04-25?12:58??八數碼問題\源程序\problem2.py
?????文件????????4708??2020-05-05?14:20??八數碼問題\源程序\problem3.py
............此處省略2個文件信息
評論
共有 條評論