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 43 44 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 84 85 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 119 120 121 122 123 124 125 126 127 128 |
#-*-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], #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], #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], #1 # [0, 0, 0, 0, 0, 0, 0, 0], #2 ## # [0, 0, 0, 0, 0, 0, 0, 0], #3 # [0, 0, 0, 0, 0, 0, 0, 0], #4 # [0, 0, 0, 0, 0, 0, 0, 0], #5 ## # [0, 0, 0, 0, 0, 0, 0, 0], #6 # [0, 0, 0, 0, 0, 0, 0, 0], #7 # [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 lines for verification 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 # Lines 4, 5, and 6 for verification 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 # Lines 7, 8, and 9 for verification 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 )) |