溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何解決python3.6數(shù)獨(dú)問(wèn)題

發(fā)布時(shí)間:2021-08-04 11:11:00 來(lái)源:億速云 閱讀:162 作者:小新 欄目:開發(fā)技術(shù)

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

如何解決python3.6數(shù)獨(dú)問(wèn)題

再試一個(gè)其他的數(shù)獨(dú):

如何解決python3.6數(shù)獨(dú)問(wèn)題

這回就快得多了,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()

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

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI