1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 |
#-*-Coding:utf-8-*-' Created on 2012-10-5 @author: Administrator ' "' from collections import defaultdict Import it Ertools 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], #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-Enumerate (a) for Y_idx, V in enumerate (y) if v) h_exist = defaultdict (dict) v_exist = Defaultdict (di CT) 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), 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): ret Urn all ([y[i] isn't 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 to L2 in h_d[2]: If not test (L0, L 1), L2): Continue # 1,2,3 line to verify if Test2 ([l0[0], l0[1], l0[2], l1[0], l1[1], l1[2], l2[0], l2[1], l2[2]): CO Ntinue 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 to L5 in h_d[5]: If Not test ((L0, L1, L2, L3, L4), L5): Continue # 4,5,6 rows to verify if Test2 ([l3[0], l3[1], l3[2], l4[0], l4[1], l4[2], l5[0], l5[1], l5[2]): Continue if test2 ([l 3[3], l3[4], l4[4], l4[5], l5[3], l5[4], l5[5]): Continue if Test2 ([l3[6], l3[7], l3[8], L 4[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 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]): Co Ntinue 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 printL4 Print L5 print L6 print L7 print L8 sudokus.append (l0, L1, L2, L3, L4, L5, L6, L7, L8) |