日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python解决数独

發(fā)布時(shí)間:2025/3/19 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python解决数独 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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"

?

轉(zhuǎn)載于:https://www.cnblogs.com/linkxu1989/p/7015636.html

總結(jié)

以上是生活随笔為你收集整理的Python解决数独的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 香港三级在线视频 | asian日本肉体pics | 午夜精品久久久久久久 | 羞羞漫画在线播放 | 一级黄色av | 91在线精品一区二区 | 亚洲综合国产精品 | 佐佐木明希电影 | av男人天堂网| 中文字幕 欧美激情 | jlzzjlzz国产精品久久 | 五月婷婷在线播放 | 日韩在线观看视频网站 | 成人福利在线观看 | 亚洲av无码精品色午夜果冻不卡 | 精品动漫一区二区三区的观看方式 | 曰韩av| 黄色一级片在线看 | 在线视频 91 | 精品国产乱码久久久久久1区二区 | 深夜福利视频导航 | 激情小说图片视频 | 久久久久这里只有精品 | 综合久久久久综合 | 亚洲精品自拍偷拍 | 精品国产无码AV | 色小说在线观看 | 国产高清视频一区二区 | 欧美成人免费一级 | 精品免费囯产一区二区三区 | xxxx色 | 91色拍 | 黑鬼大战白妞高潮喷白浆 | 免费禁漫天堂a3d | 伊人网综合在线 | 放荡闺蜜高h苏桃情事h | 日韩高清精品免费观看 | 成人免费观看视频大全 | 日韩欧美一区二区区 | 日韩和欧美的一区二区 | a级黄色一级片 | 91色噜噜| 国产毛片久久久久久国产毛片 | 影音先锋 日韩 | 骚黄网站 | 国产亚洲精品久 | 日韩一级理论片 | 侵犯女教师一区二区三区 | 日韩av第一页 | 亚洲人和日本人hd | 特级一级黄色片 | av涩涩| 大肉大捧一进一出好爽mba | 国产三极片 | 亚洲视频大全 | 久久亚洲国产精品 | 少妇媚药按摩中文字幕 | 国产美女永久免费 | 欧美人成在线 | 中日韩在线观看 | 日本视频在线观看 | 97色干| 欧美一级淫片免费视频魅影视频 | 丝袜人妻一区二区 | 国产精品亚洲AV色欲三区不卡 | 18在线观看视频 | 精品一性一色一乱农村 | 免费的av网站 | 99视频国产精品 | 人人爽人人射 | 国产视频中文字幕 | 91久久久久久久久久 | 亚洲网站视频 | 国产老熟女一区二区三区 | 日韩天堂视频 | 免费国产小视频 | 国产一级一区二区 | 日韩欧美三级 | 米奇影音 | 91精品国产综合久久精品图片 | 人人爽在线 | 中文字幕免费高清网站 | 91亚洲精品国偷拍 | 影音先锋啪啪 | 在线观看第一页 | 国产污在线观看 | 国精产品一区一区三区mba下载 | 天堂av一区二区三区 | 精品深夜av无码一区二区老年 | 天堂视频一区 | 亚洲av无码专区国产乱码不卡 | 成人网免费视频 | 亚洲精品va| 亚洲最新 | 成人免费看片在线观看 | 久久福利影院 | 国产欧美一区二区三区白浆喷水 | 国产交换配乱淫视频免费 | 国产婷婷色一区二区三区在线 |