In this paper, we describe the method of Python's implementation of Sudoku algorithm. Share to everyone for your reference. Specific as follows:
#-*-Coding:utf-8-*-"Created on 2012-10-5@author:administrator" from collections import Defaultdictimport Itertool SA = [[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], #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_id x), V) for H_idx, y in Enumerate (a) for Y_idx, V in enumerate (y) if v) h_exist = defaultdict (dict) v_exist = Defaultdict (d ICT) for K, V in Exists_d.Items (): h_exist[k[0]][k[1]] = v v_exist[k[1]][k[0]] = VAA = List (Itertools.permutations (range (1, ten), 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] = xdef Test (x, y): Return al L ([y[i] not in [x_[i] for x_ in X] for I in range (len (y))]) def test2 (x): Return len (set (x))! = 9s = 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 # A-line verification if Test2 ([l0[0], l0[1], l0[2], l1[0], l1[1], l1[2], l2[0], l2[1], L 2[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 forL3 in h_d[3]: "If not" test ((L0, L1, L2), L3): Continue for L4 on 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 row for validation i f 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]]): cont Inue 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 te St ((l0, L1, L2, L3, L4, L5, L6, L7), L8): Continue # 7,8,9 line to verify 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 PR int L7 Print L8 sudokus.append ((l0, L1, L2, L3, L4, L5, L6, L7, L8))
Hopefully this article will help you with Python programming.