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 ...
随机推荐
- 微信小程序~触摸相关事件(拖拽操作、手势识别、多点触控)
touchstart 手指触摸动作开始 touchmove 手指触摸后移动 touchcancel 手指触摸动作被打断,如来电提醒,弹窗 touchend 手指触摸动作结束 ...
- 查看linux版本及lsb_release安装及一些想法
https://blog.csdn.net/darkdragonking/article/details/61194308
- 服务器环境从PHP5升级到PHP7
#安装ppa sudo apt-get install python-software-properties software-properties-common sudo add-apt-repos ...
- 微软企业库5.0 学习之路——第七步、Cryptographer加密模块简单分析、自定义加密接口及使用—下篇
在上一篇文章中, 我介绍了企业库Cryptographer模块的一些重要类,同时介绍了企业库Cryptographer模块为我们提供的扩展接口,今天我就要根据这些 接口来进行扩展开发,实现2个加密解密 ...
- python 网络爬虫框架scrapy使用说明
1 创建项目scrapy startproject tutorial 2 定义Itemimport scrapyclass DmozItem(scrapy.Item): title = scra ...
- 将cmake文件转化为vs方便代码阅读与分析
下面通过“chengxuyuancc”同学的图来说明.通过cmake将winafl cmake编译方式转化为vs2015,方便源码阅读与分析. 1.到官网下载cmake软件.启动图形版 2.选择源码目 ...
- 洛谷P3916 图的遍历 [图论,搜索]
题目传送门 图的遍历 题目描述 给出 N 个点, M条边的有向图,对于每个点 v ,求 A(v) 表示从点 v 出发,能到达的编号最大的点. 输入输出格式 输入格式: 第1 行,2 个整数 N,M . ...
- HDU3487 Play With Chain [Splay]
题目传送门 题目描述 Problem Description YaoYao is fond of playing his chains. He has a chain containing n dia ...
- 洛谷P3369普通平衡树(Treap)
题目传送门 转载自https://www.cnblogs.com/fengzhiyuan/articles/7994428.html,转载请注明出处 Treap 简介 Treap 是一种二叉查找树.它 ...
- vue组件scoped CSS及/deep/深度选择器
参考链接:https://vue-loader.vuejs.org/zh/guide/scoped-css.html#%E5%AD%90%E7%BB%84%E4%BB%B6%E7%9A%84%E6%A ...