资源简介
利用Python程序求解任意可解数独,同时输出整个求解过程,文件中包含几个测试用例,其中有世界最难数独的求解结果

代码片段和文件信息
import os
import numpy as np
def deep_copy(nparray):
assert np.ndim(nparray) == 2 ““
shape = np.shape(nparray)
ret = np.zeros(shape dtype=np.object)
for i in range(shape[0]):
for j in range(shape[1]):
ret[i j] = nparray[i j].copy()
return ret
def get_cell_index(row col):
ret_index = [0 0]
if 3 <= row < 6:
ret_index[0] = 1
elif row >= 6:
ret_index[0] = 2
if 3 <= col < 6:
ret_index[1] = 1
elif col >= 6:
ret_index[1] = 2
return ret_index
def check_row(num array row):
if len(np.where(array[row :] == num)[0]) == 0:
return True
return False
def check_col(num array col):
if len(np.where(array[: col] == num)[0]) == 0:
return True
return False
def check_cell(num array row col):
cell_index = get_cell_index(row col)
if len(np.where(array[cell_index[0]*3:(cell_index[0]+1)*3
cell_index[1]*3:(cell_index[1]+1)*3] == num)[0]) == 0:
return True
return False
def start_init(array):
lengths = np.zeros([9 9]) # 每个位置可能填写的值的个数
may_nums = np.zeros([9 9] dtype=list) # 每个位置可能填写的所有值组成的list
for i in range(9):
for j in range(9):
if array[i j] > 0:
may_nums[i j] = []
continue
may_nums[i j] = [k for k in range(1 10)
if check_row(k array i)
and check_col(k array j)
and check_cell(k array i j)]
lengths[i j] = len(may_nums[i j])
return lengths may_nums
def update_maynums(lengths may_nums row col val):
for i in range(9):
if i == row:
continue
if val in may_nums[i col]:
may_nums[i col].remove(val)
lengths[i col] -= 1
if lengths[i col] == 0:
return True
for i in range(9):
if i == col:
continue
if val in may_nums[row i]:
may_nums[row i].remove(val)
lengths[row i] -= 1
if lengths[row i] == 0:
return True
cell_index = get_cell_index(row col)
for i in range(cell_index[0] * 3 (cell_index[0] + 1) * 3):
for j in range(cell_index[1] * 3 (cell_index[1] + 1) * 3):
if i == row and j == col:
continue
if val in may_nums[i j]:
may_nums[i j].remove(val)
lengths[i j] -= 1
if lengths[i j] == 0:
return True
return False
# return lengths may_nums
def check_only_chance_rows(array lengths may_nums per_step outf):
updated = False
wrong = False
for i in range(9):
may_nums_row = may_nums[i 0]
length = len(may_nums_row)
only_chance_index = list()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 12862 2018-12-23 00:18 sudoku_v3.py
文件 444805 2019-01-23 23:30 sudoku_inputs\hardest.res
文件 169 2018-12-22 10:41 sudoku_inputs\hardest.txt
文件 12208 2018-12-22 10:36 sudoku_inputs\大师10.res
文件 169 2018-12-22 10:36 sudoku_inputs\大师10.txt
文件 11118 2018-12-21 23:54 sudoku_inputs\大师3.res
文件 169 2018-12-21 23:36 sudoku_inputs\大师3.txt
文件 11554 2018-12-22 00:43 sudoku_inputs\大师4.res
文件 169 2018-12-21 15:43 sudoku_inputs\大师4.txt
文件 11554 2018-12-22 10:27 sudoku_inputs\大师5.res
文件 169 2018-12-22 10:18 sudoku_inputs\大师5.txt
文件 11772 2018-12-22 10:29 sudoku_inputs\大师9.res
文件 169 2018-12-22 10:29 sudoku_inputs\大师9.txt
目录 0 2019-01-23 23:30 sudoku_inputs
----------- --------- ---------- ----- ----
516887 14
- 上一篇:python程序设计基础课件
- 下一篇:12306爬虫实现
相关资源
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论