输入文件格式:

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. Go语言有缓冲和无缓冲通道实现样例

    感觉可以,但不好用. 应该有封装程序更高的包包吧. package main import ( "math/rand" "fmt" "time&quo ...

  2. [BZOJ4025] 二分图 LCT/(线段树分治+并查集)

    4025: 二分图 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2667  Solved: 989[Submit][Status][Discuss] ...

  3. css3玩转各种效果【资源】

    css3玩转各种按钮效果[资源] 点击下载 css3各种拐弯箭头-包括循环旋转 点击下载 不定期更新,下班了……

  4. 六十四 asyncio

    asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要 ...

  5. ProgrammingProjectList-文本操作

    https://github.com/jobbole/ProgrammingProjectList 逆转字符串——输入一个字符串,将其逆转并输出. package com.zrl.github; im ...

  6. Word Ladder(LintCode)

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. 【Go】基础语法之接口

    接口定义: 利用关键字interface来定义一个接口,接口是一组方法的集合. 例如: type People interface { Show(name string, age int) (id i ...

  8. Python开发基础-Day11内置函数补充、匿名函数、递归函数

    内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: divmod(a, b) #a.b为数字,a为除数,b ...

  9. [P4063][JXOI2017]数列(DP)

    题目描述 九条可怜手上有一个长度为 n 的整数数列 ri,她现在想要构造一个长度为 n 的,满足如下条件的整数数列 A: • 1 ≤ Ai ≤ ri. • 对于任意 3 ≤ i ≤ n,令 R 为 A ...

  10. YS私有通信协议安全整改方案

    1.背景: YS私有通信是hk研发的一款用于探测或设置hk设备的设备网络搜索软件,其基于hk的私有多播或广播协议实现.由于其自身拥有和hk同样长久的历史,早期在设计时未考虑到安全性因素,导致该协议存在 ...