输入文件格式:

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

 cellArray=[]
rowMax=9
columnMax=9
def pre():
with open(r"C:\Python27\sd.txt") as infile:
l=[s for s in infile]
for i in range(rowMax):
rowArray=[]
for j in range(columnMax):
c=Cell(i,j)
c.value=int(l[i][j])
rowArray.append(c)
cellArray.append(rowArray) def mynext(c):
if c.row+1<rowMax:
row=c.row+1
column=c.column
return cellArray[row][column]
elif c.column+1<columnMax:
row=0
column=c.column+1
return cellArray[row][column]
else:
return None class Cell:
def __init__(self,row,column):
self.row=row
self.column=column
self.value=0
def __str__(self):
return str(self.row)+":"+str(self.column)+":"+str(self.value) def setCellValue(cell):
if cell==None:
return True
if cell.value==0:
canList=[1,2,3,4,5,6,7,8,9]
blockCheck(canList,cell)
rowCheck(canList,cell)
columnCheck(canList,cell)
if len(canList) ==0:
return False
for canNum in canList:
cell.value=canNum
res=setCellValue(mynext(cell))
if res:
return True
cell.value=0
return False
else:
return setCellValue(mynext(cell))
def blockCheck(canList,cell):
blockrow=cell.row/3
blockcolumn=cell.column/3
for i in range(blockrow*3,(blockrow+1)*3):
for j in range(blockcolumn*3,(blockcolumn+1)*3):
cvalue=cellArray[i][j].value
if cellArray[i][j].value==0:
continue
if cvalue in canList:
canList.remove(cvalue) def rowCheck(canList,cell):
for i in range(columnMax):
cvalue=cellArray[cell.row][i].value
if cvalue==0:
continue
if cvalue in canList:
canList.remove(cvalue) def columnCheck(canList,cell):
for i in range(rowMax):
cvalue=cellArray[i][cell.column].value
if cvalue==0:
continue
if cvalue in canList:
canList.remove(cvalue) print 'yuan'
print '*'*20
pre()
for i in range(rowMax):
for j in range(columnMax):
print str(cellArray[i][j].value),
print "\n" print '*'*20
print 'result'
print '*'*20
setCellValue(cellArray[0][0])
for i in range(rowMax):
for j in range(columnMax):
print str(cellArray[i][j].value),
print "\n"

python 实现计算数独的更多相关文章

  1. windows下安装python科学计算环境,numpy scipy scikit ,matplotlib等

    安装matplotlib: pip install matplotlib 背景: 目的:要用Python下的DBSCAN聚类算法. scikit-learn 是一个基于SciPy和Numpy的开源机器 ...

  2. Python TF-IDF计算100份文档关键词权重

    上一篇博文中,我们使用结巴分词对文档进行分词处理,但分词所得结果并不是每个词语都是有意义的(即该词对文档的内容贡献少),那么如何来判断词语对文档的重要度呢,这里介绍一种方法:TF-IDF. 一,TF- ...

  3. Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)

    用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...

  4. 目前比较流行的Python科学计算发行版

    经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...

  5. Python科学计算之Pandas

    Reference: http://mp.weixin.qq.com/s?src=3&timestamp=1474979163&ver=1&signature=wnZn1UtW ...

  6. Python 科学计算-介绍

    Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...

  7. Python科学计算库

    Python科学计算库 一.numpy库和matplotlib库的学习 (1)numpy库介绍:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成 ...

  8. Python科学计算基础包-Numpy

    一.Numpy概念 Numpy(Numerical Python的简称)是Python科学计算的基础包.它提供了以下功能: 快速高效的多维数组对象ndarray. 用于对数组执行元素级计算以及直接对数 ...

  9. Python科学计算PDF

    Python科学计算(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1VYs9BamMhCnu4rfN6TG5bg 提取码:2zzk 复制这段内容后打开百度网盘手机A ...

随机推荐

  1. 何時需要重启 OFBiz

    你在做如下更改時需要重新启動OFBiz服務器: - Java文件(記得要重新編譯) - 配置/.properties文件 - entitymodel或entitygroup XML定義文件 - 服務或 ...

  2. 小米路由器3-R3 刷固件

    1.刷机前的路由器升级准备 1-1.首先进入路由器原声后台:miwifi.com 1-2.在右上角,点击系统升级.在系统版本下边选择手动升级,选择资源包里的:“miwifi_r3_all_55ac7_ ...

  3. electron调用C#应用程序实现串口通信

    最近转入零售行业开发了一系列产品,包含便利店收银软件.会员系统.供应链系统.为了追赶潮流,收银软件使用了electron平台开发,界面效果.开发效率确实不错:但是涉及到串口通讯时遇到了麻烦,elect ...

  4. 解决网页中Waiting (TTFB)数据加载过慢的问题

    解决网页中Waiting (TTFB)数据加载过慢的问题 最近做了一个网页,在本地测试良好,数据可以得到很快的反馈,但是当部署到云端Linux上时候,就会出现加载缓慢的问题.本地测试,得到数据大概3s ...

  5. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8001 -j DNAT --to-destination 172.17.0.5:8080 ! -i docker0: iptables: No chain/target/match by that name.

    在docker容器上部署项目后,启动docker容器,出现 iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dpor ...

  6. COCOS2D - JS 之JSON 解析

    list 类型的json数据  var source = ["10004","1234","4","3","1 ...

  7. Ubuntu 16.04 LTS安装Docker并使用加速器

    参考优酷:http://v.youku.com/v_show/id_XMTkxOTYwODcxNg==.html?spm=a2h0k.8191407.0.0&from=s1.8-1-1.2 首 ...

  8. Ionic-wechat项目边开发边学(四):可伸缩输入框,下拉刷新, 置顶删除

    摘要 上一篇文章主要介绍了ion-list的使用, ion-popup的使用, 通过sass自定义样式, localStorage的使用, 自定义指令和服务. 这篇文章实现的功能有消息的置顶与删除, ...

  9. python 重新执行循环中出错的那一次

    # coding: utf-8 li = [1,2,3,4,5] for num in li: while True: try: # do something except some error: c ...

  10. Mysql Sql Explain

    1.使用mysql explain的原因 在我们php程序员的日常写代码中,有时候会发现我们写的sql语句运行的特别慢,导致响应时间特别长,这种情况在高并发的情况下,我们的网站会直接崩溃,为什么双十一 ...