用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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值