python实现数独算法实例
本文实例讲述了python实现数独算法的方法。分享给大家供大家参考。具体如下:
#-*-coding:utf-8-*- ''' Createdon2012-10-5 @author:Administrator ''' fromcollectionsimportdefaultdict importitertools a=[ [0,7,0,0,0,0,0,0,0],#0 [5,0,3,0,0,6,0,0,0],#1 [0,6,2,0,8,0,7,0,0],#2 # [0,0,0,3,0,2,0,5,0],#3 [0,0,4,0,1,0,3,0,0],#4 [0,2,0,9,0,5,0,0,0],#5 # [0,0,1,0,3,0,5,9,0],#6 [0,0,0,4,0,0,6,0,3],#7 [0,0,0,0,0,0,0,2,0],#8 #0,1,2,3,|4,5,6,|7,8 ] #a=[ #[0,0,0,0,0,0,0,0,0],#0 #[0,0,0,0,0,0,0,0,0],#1 #[0,0,0,0,0,0,0,0,0],#2 ## #[0,0,0,0,0,0,0,0,0],#3 #[0,0,0,0,0,0,0,0,0],#4 #[0,0,0,0,0,0,0,0,0],#5 ## #[0,0,0,0,0,0,0,0,0],#6 #[0,0,0,0,0,0,0,0,0],#7 #[0,0,0,0,0,0,0,0,0],#8 ##0,1,2,3,|4,5,6,|7,8 #] exists_d=dict((((h_idx,y_idx),v)forh_idx,yinenumerate(a)fory_idx,vinenumerate(y)ifv)) h_exist=defaultdict(dict) v_exist=defaultdict(dict) fork,vinexists_d.items(): h_exist[k[0]][k[1]]=v v_exist[k[1]][k[0]]=v aa=list(itertools.permutations(range(1,10),9)) h_d={} forhk,hvinh_exist.items(): x=filter(lambdax:all((x[k]==vfork,vinhv.items())),aa) x=filter(lambdax:all((x[vk]!=vforvk,vvinv_exist.items()fork,vinvv.items()ifk!=hk)),x) #printx h_d[hk]=x deftest(x,y): returnall([y[i]notin[x_[i]forx_inx]foriinrange(len(y))]) deftest2(x): returnlen(set(x))!=9 s=set(range(9)) sudokus=[] forl0inh_d[0]: forl1inh_d[1]: ifnottest((l0,),l1): continue forl2inh_d[2]: ifnottest((l0,l1),l2): continue #1,2,3行进行验证 iftest2([l0[0],l0[1],l0[2] ,l1[0],l1[1],l1[2] ,l2[0],l2[1],l2[2] ]):continue iftest2([l0[3],l0[4],l0[5] ,l1[3],l1[4],l1[5] ,l2[3],l2[4],l2[5] ]):continue iftest2([l0[6],l0[7],l0[8] ,l1[6],l1[7],l1[8] ,l2[6],l2[7],l2[8] ]):continue forl3inh_d[3]: ifnottest((l0,l1,l2),l3): continue forl4inh_d[4]: ifnottest((l0,l1,l2,l3),l4): continue forl5inh_d[5]: ifnottest((l0,l1,l2,l3,l4),l5): continue #4,5,6行进行验证 iftest2([l3[0],l3[1],l3[2] ,l4[0],l4[1],l4[2] ,l5[0],l5[1],l5[2] ]):continue iftest2([l3[3],l3[4],l3[5] ,l4[3],l4[4],l4[5] ,l5[3],l5[4],l5[5] ]):continue iftest2([l3[6],l3[7],l3[8] ,l4[6],l4[7],l4[8] ,l5[6],l5[7],l5[8] ]):continue forl6inh_d[6]: ifnottest((l0,l1,l2,l3,l4,l5,),l6): continue forl7inh_d[7]: ifnottest((l0,l1,l2,l3,l4,l5,l6),l7): continue forl8inh_d[8]: ifnottest((l0,l1,l2,l3,l4,l5,l6,l7),l8): continue #7,8,9行进行验证 iftest2([l6[0],l6[1],l6[2] ,l7[0],l7[1],l7[2] ,l8[0],l8[1],l8[2] ]):continue iftest2([l6[3],l6[4],l6[5] ,l7[3],l7[4],l7[5] ,l8[3],l8[4],l8[5] ]):continue iftest2([l6[6],l6[7],l6[8] ,l7[6],l7[7],l7[8] ,l8[6],l8[7],l8[8] ]):continue printl0 printl1 printl2 printl3 printl4 printl5 printl6 printl7 printl8 sudokus.append((l0,l1,l2,l3,l4,l5,l6,l7,l8))
希望本文所述对大家的Python程序设计有所帮助。