python3.6數(shù)獨(dú)問(wèn)題的解決
算法比較暴力,直接用窮舉的方式一個(gè)一個(gè)去試,所以程序運(yùn)行時(shí)間會(huì)比較長(zhǎng),運(yùn)行時(shí)間視數(shù)獨(dú)而定。
不過(guò)從一開始到運(yùn)行成功,整個(gè)過(guò)程卻是一波三折,設(shè)計(jì)算法就花了不少時(shí)間,然后就是不斷地去調(diào)試,找bug。剛開始的時(shí)候?yàn)榱耸∈轮苯釉趕udoku類中遞歸調(diào)用blank,但是老哥還是too young too simple,sometimes navie,計(jì)算量實(shí)在是太大了,后面編譯器直接拋出 “RecursionError: maximum recursion depth exceeded while calling a Python object” 超過(guò)最大遞歸深度的錯(cuò)誤。在把遞歸深度改到100000之后,又出現(xiàn)了堆棧溢出問(wèn)題。當(dāng)然,解決辦法也是相當(dāng)?shù)乇┝Γ喊堰f歸放入while循環(huán)中,一旦符合條件就直接exit(0),整個(gè)程序直接gg,然后退出結(jié)束。
當(dāng)然,算法還可以再優(yōu)化一下,可以不用那么暴力,先列出可能的值然后再填入,這樣可以大大縮小整個(gè)程序的運(yùn)行時(shí)間,但是……懶得優(yōu)化了,就這樣吧,又不是不能用(笑~)。
運(yùn)行結(jié)果:
再試一個(gè)其他的數(shù)獨(dú):
這回就快得多了,11秒就完成了,比第一個(gè)數(shù)獨(dú)不知高到哪里去了
代碼如下所示:
import copy import time t1=time.time() origin = [[8, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 6, 0, 0, 0, 0, 0], [0, 7, 0, 0, 9, 0, 2, 0, 0], [0, 5, 0, 0, 0, 7, 0, 0, 0], [0, 0, 0, 0, 4, 5, 7, 0, 0], [0, 0, 0, 1, 0, 0, 0, 3, 0], [0, 0, 1, 0, 0, 0, 0, 6, 8], [0, 0, 8, 5, 0, 0, 0, 1, 0], [0, 9, 0, 0, 0, 0, 4, 0, 0]] class sudoku: def debug(self): # 調(diào)試 for list in origin: print(list) print("\n") def check_repetition(self,list):#判斷表中是否有重復(fù)值,0除外 flag=0 for i in range(1,10): if list.count(i)>=2: return 1 else: flag=flag+1 if flag==9: return 0 def check_row(self,row):#檢測(cè)橫向是否有重復(fù)值,無(wú)則為返回0,有則返回1 list = origin[row] # 橫向 r1 = self.check_repetition(list) if r1 == 0: return 0 else : return 1 def check_column(self,column):#檢測(cè)縱向是否重復(fù)值,無(wú)則為返回0,有則返回1 list = [] # 縱向 for num in origin: list.append(num[column]) r2 = self.check_repetition(list) if r2==0: return 0 else: return 1 def check_square(self,x,y):#檢測(cè)九宮格是否有重復(fù)值,無(wú)則為返回0,有則返回1 x,y=y,x if x>=9 or y>=9: return square = []#九宮格 for i in range(0+y//3*3, 3+y//3*3): for j in range(0+x//3*3, 3+x//3*3): square.append(origin[i][j]) r3 = self.check_repetition(square) if r3==0: return 0 else: return 1 def check(self,x,y):#檢測(cè)是否有重復(fù)值,無(wú)則為0,有則不為0 r1 = self.check_row(x) r2 = self.check_column(y) r3 = self.check_square(x, y) result=r1+r2+r3 return result def get_next(self): # 獲得下一個(gè)空值,返回row,column值 i = 0 for list in origin: try: # 當(dāng)0不在列表中時(shí),跳過(guò) column = list.index(0) row = origin.index(list) res = (row, column) return res except ValueError: i = i + 1 if i == 9: t2=time.time() print("總用時(shí)={}".format(t2 - t1)) exit(0) def poi(self,row, column): # 位置修正 if row == 0 and column == -1: return if row == 8 and column == 9: return if column == -1: column = 8 row = row - 1 if column == 9: column = 0 row = row - 1 return (row, column) def get_last(self,row, column): origin[row].insert(column, 0) origin[row].pop(column + 1) column = column - 1 # 獲得上一個(gè)已填值的行、列位置 row, column = self.poi(row, column)#位置修正 r = origin[row][column] * compare[row][column] while r != 0: column = column - 1 row, column = self.poi(row, column) r = origin[row][column] * compare[row][column] return (row, column) def blank(self): try: row,column=self.get_next() except TypeError:#已填完 exit(0) j=0 flag=0 for i in range(1,10): origin[row].insert(column,i) origin[row].pop(column+1) self.debug() r = self.check(row, column) if r==0:#無(wú)重復(fù)值 return else: j = j + 1 if j==9: flag=1 break if flag==1: row, column = self.get_last(row, column) value=origin[row][column] self.debug() while value == 9: row, column = self.get_last(row, column) value = origin[row][column] self.debug() while value<9: for k in range(value+1,10): origin[row].insert(column, k) origin[row].pop(column + 1) self.debug() r=self.check(row,column) if r!=0:#有重復(fù) if k==9: row, column = self.get_last(row, column) value=origin[row][column] self.debug() while value==9: row, column = self.get_last(row, column) value = origin[row][column] self.debug() break else: return if __name__=="__main__": compare = copy.deepcopy(origin) sudoku = sudoku() while 1: sudoku.blank()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 150行Python代碼實(shí)現(xiàn)帶界面的數(shù)獨(dú)游戲
- 用Python解數(shù)獨(dú)的方法示例
- Python判斷有效的數(shù)獨(dú)算法示例
- python實(shí)現(xiàn)自動(dòng)解數(shù)獨(dú)小程序
- python實(shí)現(xiàn)數(shù)獨(dú)游戲 java簡(jiǎn)單實(shí)現(xiàn)數(shù)獨(dú)游戲
- 簡(jiǎn)單實(shí)現(xiàn)python數(shù)獨(dú)游戲
- python實(shí)現(xiàn)解數(shù)獨(dú)程序代碼
- Python如何判斷數(shù)獨(dú)是否合法
- python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例
- Python圖像識(shí)別+KNN求解數(shù)獨(dú)的實(shí)現(xiàn)
相關(guān)文章
基于python批量處理dat文件及科學(xué)計(jì)算方法詳解
今天小編就為大家分享一篇基于python批量處理dat文件及科學(xué)計(jì)算方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Python數(shù)據(jù)可視化之Matplotlib和Seaborn的使用教程詳解
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)可視化中Matplotlib和Seaborn使用的相關(guān)教程,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-03-03python文件絕對(duì)路徑寫法介紹(windows)
今天小編就為大家分享一篇python文件絕對(duì)路徑寫法介紹(windows),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記
這篇文章主要介紹了Python實(shí)用庫(kù) PrettyTable 學(xué)習(xí)筆記,結(jié)合實(shí)例形式分析了Python表格操作庫(kù)PrettyTable的安裝、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2019-08-08python 七種郵件內(nèi)容發(fā)送方法實(shí)例
這篇文章主要介紹了python 七種郵件內(nèi)容發(fā)送方法實(shí)例,需要的朋友可以參考下2014-04-04python 3.6 tkinter+urllib+json實(shí)現(xiàn)火車車次信息查詢功能
這篇文章主要介紹了python 3.6 tkinter+urllib+json 火車車次信息查詢功能,本文以查詢火車車次至南京的信息為例,需要的朋友可以參考下2017-12-12Python?基于TCP?傳輸協(xié)議的網(wǎng)絡(luò)通信實(shí)現(xiàn)方法
網(wǎng)絡(luò)編程指在網(wǎng)絡(luò)環(huán)境中,如何實(shí)現(xiàn)不在同一物理位置中的計(jì)算機(jī)之間進(jìn)行數(shù)據(jù)通信,本文重點(diǎn)給大家介紹Python?基于TCP?傳輸協(xié)議的網(wǎng)絡(luò)通信實(shí)現(xiàn)方法,感興趣的朋友跟隨小編一起看看吧2022-02-02