免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

如何解決python3.6數(shù)獨問題-創(chuàng)新互聯(lián)

這篇文章主要為大家展示了“如何解決python3.6數(shù)獨問題”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“如何解決python3.6數(shù)獨問題”這篇文章吧。

成都創(chuàng)新互聯(lián)是專業(yè)的巴林左旗網(wǎng)站建設(shè)公司,巴林左旗接單;提供成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行巴林左旗網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

算法比較暴力,直接用窮舉的方式一個一個去試,所以程序運行時間會比較長,運行時間視數(shù)獨而定。
不過從一開始到運行成功,整個過程卻是一波三折,設(shè)計算法就花了不少時間,然后就是不斷地去調(diào)試,找bug。剛開始的時候為了省事直接在sudoku類中遞歸調(diào)用blank,但是老哥還是too young too simple,sometimes navie,計算量實在是太大了,后面編譯器直接拋出 “RecursionError: maximum recursion depth exceeded while calling a Python object” 超過大遞歸深度的錯誤。在把遞歸深度改到100000之后,又出現(xiàn)了堆棧溢出問題。當然,解決辦法也是相當?shù)乇┝Γ喊堰f歸放入while循環(huán)中,一旦符合條件就直接exit(0),整個程序直接gg,然后退出結(jié)束。
當然,算法還可以再優(yōu)化一下,可以不用那么暴力,先列出可能的值然后再填入,這樣可以大大縮小整個程序的運行時間,但是……懶得優(yōu)化了,就這樣吧,又不是不能用(笑~)。

運行結(jié)果:

如何解決python3.6數(shù)獨問題

再試一個其他的數(shù)獨:

如何解決python3.6數(shù)獨問題

這回就快得多了,11秒就完成了,比第一個數(shù)獨不知高到哪里去了

代碼如下所示:

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):#檢測橫向是否有重復(fù)值,無則為返回0,有則返回1
 list = origin[row] # 橫向
 r1 = self.check_repetition(list)
 if r1 == 0:
  return 0
 else :
  return 1

 def check_column(self,column):#檢測縱向是否重復(fù)值,無則為返回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):#檢測九宮格是否有重復(fù)值,無則為返回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):#檢測是否有重復(fù)值,無則為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): # 獲得下一個空值,返回row,column值
 i = 0
 for list in origin:
  try: # 當0不在列表中時,跳過
  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("總用時={}".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 # 獲得上一個已填值的行、列位置
 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:#無重復(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()

以上是“如何解決python3.6數(shù)獨問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站欄目:如何解決python3.6數(shù)獨問題-創(chuàng)新互聯(lián)
地址分享:http://newbst.com/article26/dooscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google網(wǎng)站策劃外貿(mào)網(wǎng)站建設(shè)建站公司網(wǎng)站建設(shè)定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)