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
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 实现计算数独的更多相关文章
- windows下安装python科学计算环境,numpy scipy scikit ,matplotlib等
安装matplotlib: pip install matplotlib 背景: 目的:要用Python下的DBSCAN聚类算法. scikit-learn 是一个基于SciPy和Numpy的开源机器 ...
- Python TF-IDF计算100份文档关键词权重
上一篇博文中,我们使用结巴分词对文档进行分词处理,但分词所得结果并不是每个词语都是有意义的(即该词对文档的内容贡献少),那么如何来判断词语对文档的重要度呢,这里介绍一种方法:TF-IDF. 一,TF- ...
- Python科学计算(二)windows下开发环境搭建(当用pip安装出现Unable to find vcvarsall.bat)
用于科学计算Python语言真的是amazing! 方法一:直接安装集成好的软件 刚开始使用numpy.scipy这些模块的时候,图个方便直接使用了一个叫做Enthought的软件.Enthought ...
- 目前比较流行的Python科学计算发行版
经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...
- Python科学计算之Pandas
Reference: http://mp.weixin.qq.com/s?src=3×tamp=1474979163&ver=1&signature=wnZn1UtW ...
- Python 科学计算-介绍
Python 科学计算 作者 J.R. Johansson (robert@riken.jp) http://dml.riken.jp/~rob/ 最新版本的 IPython notebook 课程文 ...
- Python科学计算库
Python科学计算库 一.numpy库和matplotlib库的学习 (1)numpy库介绍:科学计算包,支持N维数组运算.处理大型矩阵.成熟的广播函数库.矢量运算.线性代数.傅里叶变换.随机数生成 ...
- Python科学计算基础包-Numpy
一.Numpy概念 Numpy(Numerical Python的简称)是Python科学计算的基础包.它提供了以下功能: 快速高效的多维数组对象ndarray. 用于对数组执行元素级计算以及直接对数 ...
- Python科学计算PDF
Python科学计算(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1VYs9BamMhCnu4rfN6TG5bg 提取码:2zzk 复制这段内容后打开百度网盘手机A ...
随机推荐
- memcached安装+绑定访问ip
安装: 1.由于memcached是基于libevent的,需要安装libevent,libevent-devel $yum -y install libevent libevent-devel 2. ...
- 微擎系统BUG漏洞解决方法汇总(原创)
微擎微赞系统BUG漏洞解决方法汇总 弄了微擎系统来玩玩,发觉这个系统BUG还不少,阿里云的提醒都一大堆,主要是没有针对SQL注入做预防,处理的办法基本都是用转义函数. 汇总: 1. 漏洞名称: 微擎任 ...
- hdu5756
http://www.cnblogs.com/duoxiao/p/5777644.html 官方题解在这里 其实这道题不难,当初训练的时候不会做说明自己太弱 lazy标记不pushdown就是用laz ...
- mysql source 乱码
mysql -u root -p --default-character-set=utf8 use dbname source /root/newsdata.sql
- 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]
题目传送门 数字序列 题目描述 给定一个整数序列 a1,a2,⋅⋅⋅,an ,求出一个递增序列 b1<b2<⋅⋅⋅<bn ,使得序列 ai 和 bi 的各项之差的绝对 ...
- Luogu P2486 染色(树链剖分+线段树)
题解 不妨采取重链剖分的方式把路径剖成区间,然后用线段树维护,考虑如何合并一个区间 struct Node { int lf, rg, tot; }seg[N << 2]; int col ...
- 【BZOJ 1998】 1998: [Hnoi2010]Fsk物品调度(双向链表+并查集+置换)
1998: [Hnoi2010]Fsk物品调度 Description 现在找工作不容易,Lostmonkey费了好大劲才得到fsk公司基层流水线操作员的职位.流水线上有n个位置,从0到n-1依次编号 ...
- noip 2016 day1 T1玩具谜题
题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...
- hdu 4489 The King’s Ups and Downs(基础dp)
The King’s Ups and Downs Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- 【计算几何】【极角排序】Gym - 101174B - Bribing Eve
把每件物品当成平面上一个点,将第一件物品放在原点.那个权重值相当于一条直线,于是相当于直线绕原点转一圈,统计上侧点的数量. 队友的代码: #include <cmath> #include ...