日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python数独代码_python 实现计算数独

發布時間:2023/12/10 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python数独代码_python 实现计算数独 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輸入文件格式:

008309100

900060004

007504800

036000540

001000600

042000970

005907300

600010008

004608200

輸出結果:

yuan

********************

0 0 8 3 0 9 1 0 0

9 0 0 0 6 0 0 0 4

0 0 7 5 0 4 8 0 0

0 3 6 0 0 0 5 4 0

0 0 1 0 0 0 6 0 0

0 4 2 0 0 0 9 7 0

0 0 5 9 0 7 3 0 0

6 0 0 0 1 0 0 0 8

0 0 4 6 0 8 2 0 0

********************

result

********************

4 2 8 3 7 9 1 6 5

9 5 3 8 6 1 7 2 4

1 6 7 5 2 4 8 3 9

8 3 6 7 9 2 5 4 1

7 9 1 4 3 5 6 8 2

5 4 2 1 8 6 9 7 3

2 8 5 9 4 7 3 1 6

6 7 9 2 1 3 4 5 8

3 1 4 6 5 8 2 9 7

1 cellArray=[]2 rowMax=9

3 columnMax=9

4 defpre():5 with open(r"C:\Python27\sd.txt") as infile:6 l=[s for s ininfile]7 for i inrange(rowMax):8 rowArray=[]9 for j inrange(columnMax):10 c=Cell(i,j)11 c.value=int(l[i][j])12 rowArray.append(c)13 cellArray.append(rowArray)14

15 defmynext(c):16 if c.row+1<17 row="c.row+1</p">

18 column=c.column19 returncellArray[row][column]20 elif c.column+1<21 row="022" column="c.column+1</p">

23 returncellArray[row][column]24 else:25 returnNone26

27 classCell:28 def __init__(self,row,column):29 self.row=row30 self.column=column31 self.value=032 def __str__(self):33 return str(self.row)+":"+str(self.column)+":"+str(self.value)34

35 defsetCellValue(cell):36 if cell==None:37 returnTrue38 if cell.value==0:39 canList=[1,2,3,4,5,6,7,8,9]40 blockCheck(canList,cell)41 rowCheck(canList,cell)42 columnCheck(canList,cell)43 if len(canList) ==0:44 returnFalse45 for canNum incanList:46 cell.value=canNum47 res=setCellValue(mynext(cell))48 ifres:49 returnTrue50 cell.value=051 returnFalse52 else:53 returnsetCellValue(mynext(cell))54 defblockCheck(canList,cell):55 blockrow=cell.row/3

56 blockcolumn=cell.column/3

57 for i in range(blockrow*3,(blockrow+1)*3):58 for j in range(blockcolumn*3,(blockcolumn+1)*3):59 cvalue=cellArray[i][j].value60 if cellArray[i][j].value==0:61 continue

62 if cvalue incanList:63 canList.remove(cvalue)64

65 defrowCheck(canList,cell):66 for i inrange(columnMax):67 cvalue=cellArray[cell.row][i].value68 if cvalue==0:69 continue

70 if cvalue incanList:71 canList.remove(cvalue)72

73 defcolumnCheck(canList,cell):74 for i inrange(rowMax):75 cvalue=cellArray[i][cell.column].value76 if cvalue==0:77 continue

78 if cvalue incanList:79 canList.remove(cvalue)80

81 print 'yuan'

82 print '*'*20

83 pre()84 for i inrange(rowMax):85 for j inrange(columnMax):86 printstr(cellArray[i][j].value),87 print "\n"

88

89 print '*'*20

90 print 'result'

91 print '*'*20

92 setCellValue(cellArray[0][0])93 for i inrange(rowMax):94 for j inrange(columnMax):95 printstr(cellArray[i][j].value),96 print "\n"

21>17>

總結

以上是生活随笔為你收集整理的python数独代码_python 实现计算数独的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。