最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 14:41:05
文檔

python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例

python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例:本文實(shí)例講述了python實(shí)現(xiàn)數(shù)獨(dú)算法的方法。分享給大家供大家參考。具體如下: # -*- coding: utf-8 -*- ''' Created on 2012-10-5 @author: Administrator ''' from collections import defaultdict import it
推薦度:
導(dǎo)讀python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例:本文實(shí)例講述了python實(shí)現(xiàn)數(shù)獨(dú)算法的方法。分享給大家供大家參考。具體如下: # -*- coding: utf-8 -*- ''' Created on 2012-10-5 @author: Administrator ''' from collections import defaultdict import it

本文實(shí)例講述了python實(shí)現(xiàn)數(shù)獨(dú)算法的方法。分享給大家供大家參考。具體如下:

# -*- coding: utf-8 -*-
'''
Created on 2012-10-5
@author: Administrator
'''
from collections import defaultdict
import itertools
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) for h_idx, y in enumerate(a) for y_idx , v in enumerate(y) if v))
h_exist = defaultdict(dict)
v_exist = defaultdict(dict)
for k, v in exists_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 = {}
for hk, hv in h_exist.items():
 x = filter(lambda x:all((x[k] == v for k, v in hv.items())), aa)
 x = filter(lambda x:all((x[vk] != v for vk , vv in v_exist.items() for k, v in vv.items() if k != hk)), x)
# print x
 h_d[hk] = x
def test(x, y):
 return all([y[i] not in [x_[i] for x_ in x] for i in range(len(y)) ])
def test2(x):
 return len(set(x)) != 9
s = set(range(9))
sudokus = []
for l0 in h_d[0 ]:
 for l1 in h_d[ 1]:
 if not test((l0,), l1):
 continue
 for l2 in h_d[ 2]:
 if not test((l0, l1), l2):
 continue
 # 1,2,3行 進(jìn)行驗(yàn)證
 if test2([l0[ 0], l0[ 1], l0[ 2]
 , l1[ 0], l1[ 1], l1[ 2]
 , l2[ 0], l2[ 1], l2[ 2]
 ]) : continue 
 if test2([l0[ 3], l0[ 4], l0[ 5]
 , l1[ 3], l1[ 4], l1[ 5]
 , l2[ 3], l2[ 4], l2[ 5]
 ]) : continue 
 if test2([l0[ 6], l0[ 7], l0[ 8]
 , l1[ 6], l1[ 7], l1[ 8]
 , l2[ 6], l2[ 7], l2[ 8]
 ]) : continue 
 for l3 in h_d[ 3]:
 if not test((l0, l1, l2), l3):
 continue
 for l4 in h_d[ 4]:
 if not test((l0, l1, l2, l3), l4):
 continue
 for l5 in h_d[ 5]:
 if not test((l0, l1, l2, l3, l4), l5):
 continue
 # 4,5,6行 進(jìn)行驗(yàn)證
 if test2([l3[ 0], l3[ 1], l3[ 2]
 , l4[ 0], l4[ 1], l4[ 2]
 , l5[ 0], l5[ 1], l5[ 2]
 ]) : continue 
 if test2([l3[ 3], l3[ 4], l3[ 5]
 , l4[ 3], l4[ 4], l4[ 5]
 , l5[ 3], l5[ 4], l5[ 5]
 ]) : continue 
 if test2([l3[ 6], l3[ 7], l3[ 8]
 , l4[ 6], l4[ 7], l4[ 8]
 , l5[ 6], l5[ 7], l5[ 8]
 ]) : continue 
 for l6 in h_d[ 6]:
 if not test((l0, l1, l2, l3, l4, l5,), l6):
 continue
 for l7 in h_d[ 7]:
 if not test((l0, l1, l2, l3, l4, l5, l6), l7):
 continue
 for l8 in h_d[ 8]:
 if not test((l0, l1, l2, l3, l4, l5, l6, l7), l8):
 continue
 # 7,8,9行 進(jìn)行驗(yàn)證
 if test2([l6[ 0], l6[ 1], l6[ 2]
 , l7[0 ], l7[1 ], l7[2 ]
 , l8[0 ], l8[1 ], l8[2 ]
 ]) : continue 
 if test2([l6[ 3], l6[ 4], l6[ 5]
 , l7[3 ], l7[4 ], l7[5 ]
 , l8[3 ], l8[4 ], l8[5 ]
 ]) : continue 
 if test2([l6[ 6], l6[ 7], l6[ 8]
 , l7[6 ], l7[7 ], l7[8 ]
 , l8[6 ], l8[7 ], l8[8 ]
 ]) : continue 
 print l0
 print l1
 print l2
 print l3
 print l4
 print l5
 print l6
 print l7
 print l8
 sudokus.append((l0, l1, l2, l3, l4, l5, l6, l7, l8))

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例

python實(shí)現(xiàn)數(shù)獨(dú)算法實(shí)例:本文實(shí)例講述了python實(shí)現(xiàn)數(shù)獨(dú)算法的方法。分享給大家供大家參考。具體如下: # -*- coding: utf-8 -*- ''' Created on 2012-10-5 @author: Administrator ''' from collections import defaultdict import it
推薦度:
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top