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 ...
随机推荐
- 解决CentOS7.4KDE桌面或者gnome桌面安装VLC及声音问题
一.安装VLC 1.下载源 https://mirrors.tuna.tsinghua.edu.cn/epel/7/x86_64/e/epel-release-7-11.noarch.rpm http ...
- Ljava/lang/Iterable与AbstractMethodError
java.lang.AbstractMethodError: com.example.demo.repository.UserRepositoryImpl.findAll()Ljava/lang/It ...
- 用python正则表达式提取网页的url
import re import urllib url="http://www.itokit.com" s=urllib.urlopen(url).read() ss=s.repl ...
- 基于SAAJ的客户端
概述 SAAJ - SOAP with Attachments API for JAVA 结构图如下: 正文 1. 如何获取soap请求的关键参数 关键的参数有四个: xmlns - xml命名空间如 ...
- Python开发基础-Day19继承组合应用、对象序列化和反序列化,选课系统综合示例
继承+组合应用示例 class Date: #定义时间类,包含姓名.年.月.日,用于返回生日 def __init__(self,name,year,mon,day): self.name = nam ...
- Python开发基础-Day11内置函数补充、匿名函数、递归函数
内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: divmod(a, b) #a.b为数字,a为除数,b ...
- [ARC097F]Monochrome Cat
题意:一棵树,每个节点是黑色或白色,你可以从任意节点开始进行一些操作并在任意节点结束,如果当前在$x$,那么一次操作可以是:1.走到相邻节点$y$并翻转$y$的颜色,2.翻转$x$的颜色,问把所有节点 ...
- Problem D: 零起点学算法94——输出矩阵
#include<stdio.h> int main() { ][]; while(scanf("%d %d",&n,&m)!=EOF) { ; ;i& ...
- UDP和TCP的区别和联系
UDP特点 将数据源和目的封装在数据包中,不需要简历连接 每个数据报的大小在限制在64K内 因无连接,是不可靠协议 不需要建立连接,速度快 TCP 建立连接.形成传输数据通道. 在连接中进行大量数据量 ...
- Educational Codeforces Round 8 C. Bear and String Distance 贪心
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...