Environment: Python27

# -*- coding: UTF-8 -*-
'''
Created on 2017年6月9日@author: LXu4
'''
import copy
import time
class Soduku(object):def __init__(self, problem):self.problem = problemdef resolve(self):solutionStack = [self.problem]tmp = self.get_solution_array(self.problem)solutionArrayStack = [tmp]time_t = 0prev_x = -1prev_y = -1while 1:# time_t += 1# fetch the last solution in solution stacknext_item_cord = {}solutionArray = []# print 'still ',len(solutionStack),'in stack'solutionNow = copy.deepcopy(solutionStack[len(solutionStack) - 1])solutionArray = solutionArrayStack[len(solutionArrayStack) - 1]flag = self.check_if_need_to_back(solutionNow, solutionArray)if flag is True:# print 'pop!'
                solutionArrayStack.pop()solutionStack.pop()else:time_t += 1# next_item_cord = self.get_first_possible_item(solutionArray,solutionNow=solutionNow)next_item_cord = self.get_first_possible_item(solutionArray,solutionNow=solutionNow)if next_item_cord == False:break# print 'next_item_cord:',next_item_cordprev_x = next_item_cord['x']prev_y = next_item_cord['y']next_item_array = solutionArray[prev_x][prev_y]next_item = next_item_array[len(next_item_array)-1]# randint(0,len(next_item_array)-1)solutionNow[prev_x][prev_y] = next_item# solutionArray_tmp = get_solution_array(solutionNow)solutionArray_tmp = copy.deepcopy(solutionArray)solutionArray_tmp = self.get_resolution_array_new(solutionArray_tmp, prev_x, prev_y,next_item)if next_item in solutionArray[prev_x][prev_y]:solutionArray[prev_x][prev_y].remove(next_item)# print 'next point is ',prev_x,',',prev_y
                solutionStack.append(solutionNow)solutionArrayStack.append(solutionArray_tmp)# print solutionArrayStack# print solutionStackfor i in range(0, 9, 1):print solutionStack[len(solutionStack) - 1][i]print 'total forward:',time_tdef check_if_need_to_back(self,solutionNow, solutionArray):for i in range(0, 9, 1):for j in range(0, 9, 1):if len(solutionArray[i][j]) == 0 and solutionNow[i][j] == 0:return Truereturn Falsedef get_resolution_array_new(self,solutionArray, x, y, value):for tmp_j in range(0, 9, 1):if value in solutionArray[x][tmp_j]:solutionArray[x][tmp_j].remove(value)for tmp_i in range(0, 9, 1):if value in solutionArray[tmp_i][y]:solutionArray[tmp_i][y].remove(value)for tmp_i in range(x / 3 * 3, x / 3 * 3 + 3):for tmp_j in range(y / 3 * 3, y / 3 * 3 + 3):if value in solutionArray[tmp_i][tmp_j]:solutionArray[tmp_i][tmp_j].remove(value)return solutionArraydef get_solution_array(self,problem):tmp = []for i in range(0, 9, 1):tmp_line_array = []for j in range(0, 9, 1):# print '['+bytes(i)+','+bytes(j)+']: '+ bytes(problem[i][j])if problem[i][j] == 0:# no value, get possible value arraytmp_value = [1, 2, 3, 4, 5, 6, 7, 8, 9]# remove the existed value in linefor tmp_j in range(0, 9, 1):if problem[i][tmp_j] != 0:if problem[i][tmp_j] in tmp_value:tmp_value.remove(problem[i][tmp_j])# remove the existed value in columnfor tmp_i in range(0, 9, 1):if problem[tmp_i][j] != 0:if problem[tmp_i][j] in tmp_value:tmp_value.remove(problem[tmp_i][j])# remove the existed value in the rectanglefor x in range(i / 3 * 3, i / 3 * 3 + 3):for y in range(j / 3 * 3, j / 3 * 3 + 3):if problem[x][y] != 0:if problem[x][y] in tmp_value:tmp_value.remove(problem[x][y])tmp_line_array.append(tmp_value)else:tmp_line_array.append([])tmp.append(tmp_line_array)# print tmp_line_array# print tmpreturn tmp# get first item to be the point of treedef get_first_possible_item(self, solution_array, solutionNow = None):is_finished = Trueshortest_item_length = 9shortest_item_x = 0shortest_item_y = 0for i in range(0, 9, 1):for j in range(0, 9, 1):tmp_length = len(solution_array[i][j])if tmp_length != 0:is_finished = Falseif solutionNow[i][j] != 0:tmp_length += 1if tmp_length < shortest_item_length:shortest_item_length = tmp_lengthshortest_item_x = ishortest_item_y = j# print 'shortest item is:',shortest_item_length,shortest_item_x,shortest_item_yif is_finished:return Falseelse:return {'x': shortest_item_x, 'y': shortest_item_y}def get_next_possible_item(self, solution_array, prev_x, prev_y, solutionNow = None):if prev_x == -1 and prev_y == -1:return self.get_first_possible_item(solution_array, solutionNow)else:is_finished = Trueshortest_item_length = 9shortest_item_x = 0shortest_item_y = 0for tmp_i in range(0, 9, 1):tmp_length = len(solution_array[tmp_i][prev_y])if tmp_length != 0:is_finished = Falseif solutionNow[tmp_i][prev_y] != 0:tmp_length += 1if tmp_length < shortest_item_length:shortest_item_length = tmp_lengthshortest_item_x = tmp_ishortest_item_y = prev_yif tmp_length == 1:return {'x': shortest_item_x, 'y': shortest_item_y}for tmp_j in range(0, 9, 1):tmp_length = len(solution_array[prev_x][tmp_j])if tmp_length != 0:is_finished = Falseif solutionNow[prev_x][tmp_j] != 0:tmp_length += 1if tmp_length < shortest_item_length:shortest_item_length = tmp_lengthshortest_item_x = prev_xshortest_item_y = tmp_jif tmp_length == 1:return {'x': shortest_item_x, 'y': shortest_item_y}for x in range(prev_x / 3 * 3, prev_x / 3 * 3 + 3):for y in range(prev_y / 3 * 3, prev_y / 3 * 3 + 3):tmp_length = len(solution_array[x][y])if tmp_length != 0:is_finished = Falseif solutionNow[x][y] != 0:tmp_length += 1if tmp_length < shortest_item_length:shortest_item_length = tmp_lengthshortest_item_x = xshortest_item_y = yif tmp_length == 1:return {'x': shortest_item_x, 'y': shortest_item_y}# print 'shortest item is:',shortest_item_length,shortest_item_x,shortest_item_yif is_finished:return self.get_first_possible_item(solution_array,solutionNow)else:return {'x': shortest_item_x, 'y': shortest_item_y}problem = \[[3,0,8,0,0,0,6,0,0],  [0,4,0,0,6,5,0,0,7],  [7,0,0,4,3,0,0,9,0],  [0,0,7,0,0,1,5,8,0],  [1,0,0,0,2,0,0,0,9],  [0,9,4,7,0,0,2,0,0],  [8,0,0,0,7,4,0,0,0],  [4,0,0,6,5,0,8,1,0],  [0,0,9,0,0,0,7,0,2]      ]f = Soduku(problem)
startTime=time.time()
f.resolve()
endTime=time.time()
print "Finished! Time consuming: " + "%.4f" % (endTime-startTime) + " Seconds"

转载于:https://www.cnblogs.com/linkxu1989/p/7015636.html

Python解决数独相关推荐

  1. 使用python解决数独问题

    数独规则: 在一个9 X 9的二维矩阵中,每一行(Row).每一列(Col)和每个宫(House)均不存在相同的数字 设计思路: 把九宫格设计为一个二维列表,通过行列坐标来定位数字. 1:添加函数,作 ...

  2. python解决数独问题_Python-如何消除数独方块中的凸度缺陷?

    小编典典 我有一个可行的解决方案,但是你必须自己将其转换为OpenCV.它用Mathematica编写. 第一步是通过将每个像素除以关闭操作的结果来调整图像的亮度: src = ColorConver ...

  3. 启发式搜索算法解决数独问题sudoku,附python实现

    定义 数独是源自18世纪瑞士的一种数学游戏.是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫(3*3)内的数字均含1 ...

  4. 摒弃法解决数独问题sudoku,附python实现

    定义 数独是源自18世纪瑞士的一种数学游戏.是一种运用纸.笔进行演算的逻辑游戏.玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫(3*3)内的数字均含1 ...

  5. delphi dbgrideh 遍历每一个单元格_用Python解数独[1]:求每个单元格的行值域

    目录 用Python解数独[0] 用Python解数独[1]:求每个单元格的行值域 用Python解数独[2]:求列值域和九宫格值域 用Python解数独[3]:求总值域 用Python解数独[4]: ...

  6. python deepcopy函数_用Python解数独[6]:递归获得最终答案

    目录 用Python解数独[0] 用Python解数独[1]:求每个单元格的行值域 用Python解数独[2]:求列值域和九宫格值域 用Python解数独[3]:求总值域 用Python解数独[4]: ...

  7. VBA:完美解决数独问题----全网最强

    [说在前面]: 之前,我在微信朋友圈看到一个同事发了一个状态,说的是她在家辅导孩子做作业,一个数独的题目,好像没有做出来.我看了下,我也做不出来,后来仔细想了下,花了两个多小时时间,用Python编了 ...

  8. python可以数独游戏吗_学习python编程如何一键做出数独游戏答案 | k12教育网

    在学习python编程如何一键做出数独游戏答案之前,我们需要知道什么是Python? Python指的是Python编程语言(用于编写被认为是有效Python代码的语法规则)和Python解释器软件, ...

  9. 解决数独问题用人工智能还是量子计算?

    作为一种有趣的棋盘游戏,数独诞生100周年之后,它是如何成为计算研究的焦点之一的呢?探索如何使用人工智能或量子计算机从头开始创建一个智能数独求解器. 在深入探究之前,先来了解一下历史 马克•布洛赫说: ...

最新文章

  1. LVS原理详解以及部署
  2. RabbitMQ学习总结(3)——入门实例教程详解
  3. mapreduce编程实例(2)-求最大值和最小值
  4. Python的常用模块
  5. android 切换排列,在运行时重新排序android线性布局?
  6. 弹性碰撞后速度方向_高三期中考后分析,不得不知道的重难点
  7. matlab拟合参数最优,使用matlab最优化方法拟合获得多个动力学参数中的问题 - 计算模拟 - 小木虫 - 学术 科研 互动社区...
  8. 一起学React--组件定义和组件通讯
  9. IOT---(3)深入解析物联网操作系统(架构、功能与实例分析)
  10. vivo NEX 3S 5G今日登场:无界瀑布屏加持 世界随处可及
  11. 计算机屏幕调节亮度,电脑屏幕亮度怎么调最好 有没有调节电脑屏幕亮度的软件...
  12. Kubernetes入门——Longhorn简介
  13. 计算机系统是无形资产吗,计算机操作系统做为无形资产核算吗
  14. 怎样才能无需训练也能成为武林高手
  15. 中国十大最具影响力黑客
  16. MS sqlserver数据库恢复出错 Exclusive access could not be obtained because the database is in use
  17. 餐厅点菜c语言程序代码,餐馆点菜系统C语言源代码.pdf
  18. bmi计算 python_Python中的BMI计算器
  19. 屏蔽跑跑卡丁车结束后的广告
  20. 使用 matplotlib绘制简单图形,保存图片的方法

热门文章

  1. python在办公上的应用_Python 应用 办公自动化之 Excel(上)
  2. dell笔记本电脑驱动_笔记本电脑摄像头打不开,怎么办?总结经验(基于摄像头本身是完好的)...
  3. gps数据存储mysql_gps数据存储mysql
  4. 编程设计思路怎么写_赣州脐橙朋友圈文案怎么写?思路适合任何卖产品
  5. eq linux_在线试用 200 多种 Linux 和 Unix 操作系统 | Linux 中国
  6. unicode_literals导致的UnicodeEncodeError
  7. xgb多线程成功运行记录
  8. 最大间隔分类器的错误理解
  9. 大厂程序员年薪_程序员羡慕深圳老师的待遇:年薪25万起,请问哪个科技厂可以比...
  10. python批量处理csv_Python批量处理csv并保存过程代码解析