Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.13 KB | None | 0 0
  1. #r = "800000000003600000070090200050007000000045700000100030001000068008500010090000400"
  2. #r = "100007090030020008009600500005300900010080002600004000300000010040000007007000300"
  3. #r = "000308000000000000306000901900504002005297300000000000080602010460000097102000805"
  4. r = "040006000600320590702000080069010000000809000000070960090000105013052009000400020"
  5.  
  6. b_org = map(int, list(r))
  7. b = map(int, list(r))
  8.  
  9. def print_board(b):
  10.   for i in range(9):
  11.     print "%s%s%s %s%s%s %s%s%s" % tuple(b[i*9:i*9+9])
  12.     if i in (2, 5):
  13.       print
  14.   print
  15.  
  16. def h_neighbours(z):
  17.   o = (z//9)*9
  18.   l = [o+i for i in range(9)]
  19.   return l
  20.  
  21. def v_neighbours(z):
  22.   o = z%9
  23.   l = [o+i*9 for i in range(9)]
  24.   return l
  25.  
  26. def b_neighbours(z):
  27.   x, y = (z//3)%3, (z//27)
  28.   o = x*3+y*27
  29.   l = [o+i for i in (0, 1, 2, 9, 10, 11, 18, 19, 20)]
  30.   return l
  31.  
  32. def all_neighbours(z):
  33.   l = set(h_neighbours(z) + v_neighbours(z) + b_neighbours(z))
  34.   l.remove(z)
  35.   return l
  36.  
  37. def placable(b, z, n):
  38.   if b[z] != 0: return False
  39.   for p in all_neighbours(z):
  40.     if b[p] == n:
  41.       return False
  42.   return True
  43.  
  44. def possible_list(b, z):
  45.   return  [i for i in range(1,10) if placable(b, z, i)]
  46.  
  47.  
  48. def get_placables_and_choices(b):
  49.   _t = []
  50.   for i in range(81):
  51.     l = possible_list(b, i)
  52.     if len(l) > 0:
  53.       _t.append((len(l), i, possible_list(b, i)))
  54.   _t.sort()
  55.  
  56.   placables = [i[1] for i in _t]
  57.   choices = {}
  58.   for i in _t:
  59.     choices[i[1]] = i[2]
  60.  
  61.   return placables, choices
  62.  
  63.  
  64.  
  65. while 1:
  66.   print_board(b)
  67.  
  68.   placables, choices = get_placables_and_choices(b)
  69.  
  70.   determined = set()
  71.  
  72.   for place in placables:
  73.     if len(choices[place]) == 1:
  74.       determined.add((place, choices[place][0]))
  75.       continue
  76.     for fn in (h_neighbours, v_neighbours, b_neighbours):
  77.       tb = []
  78.       for i in fn(place):
  79.         tb += choices.get(i, [])
  80.       for c in choices[place]:
  81.         if tb.count(c) == 1:
  82.           determined.add((place, c))
  83.  
  84.   #print placables
  85.   print choices
  86.   print determined
  87.   if not determined: break
  88.   for at, num in determined: b[at] = num
  89.  
  90.  
  91.  
  92. """
  93. import random
  94.  
  95. rel_off = [0, 1, 2, 9, 10, 11, 18, 19, 20]
  96. blk_off = [0, 3, 6, 27, 30, 33, 54, 57, 60]
  97.  
  98.  
  99. def print_board(b):
  100.  for i in range(9):
  101.    print "%s%s%s %s%s%s %s%s%s" % tuple(b[i*9:i*9+9])
  102.    if i in (2, 5):
  103.      print
  104.  print ""
  105.  
  106.  
  107.  
  108. #for i in range(9):
  109. #  for d in io:
  110. #    b[so[i]+d] = i
  111.  
  112. def horizontal_ok(b, z, n):
  113.  o = (z//9)*9
  114.  for i in range(9):
  115.    if b[o+i] == n:
  116.      return False
  117.  return True
  118.  
  119. def vertical_ok(b, z, n):
  120.  o = z%9
  121.  for i in range(9):
  122.    if b[o+i*9] == n:
  123.      return False
  124.  return True
  125.  
  126. c = 1
  127.  
  128. while 1:
  129.  b = [0,]*81
  130.  for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
  131.    for i in range(9):
  132.      random.shuffle(rel_off)
  133.      for j in range(9):
  134.        z = blk_off[i] + rel_off[j]
  135.        if b[z] != 0: continue
  136.        if horizontal_ok(b, z, n) and vertical_ok(b, z, n):
  137.          b[z] = n
  138.          break
  139.  #print_board(b)
  140.  if b.count(0) == 0: break
  141.  c += 1
  142.  
  143. print_board(b)
  144. #print c
  145. """
  146.  
  147.  
  148.  
  149. """
  150. r = "800000000003600000070090200050007000000045700000100030001000068008500010090000400"
  151. #r = "100007090030020008009600500005300900010080002600004000300000010040000007007000300"
  152.  
  153. b_org = map(int, list(r))
  154. b = map(int, list(r))
  155.  
  156. rel_off = [0, 1, 2, 9, 10, 11, 18, 19, 20]
  157. blk_off = [0, 3, 6, 27, 30, 33, 54, 57, 60]
  158.  
  159.  
  160. def print_board(b):
  161.  for i in range(9):
  162.    print "%s%s%s %s%s%s %s%s%s" % tuple(b[i*9:i*9+9])
  163.    if i in (2, 5):
  164.      print
  165.  print ""
  166.  
  167. def horizontal_ok(b, z, n):
  168.  o = (z//9)*9
  169.  for i in range(9):
  170.    if b[o+i] == n:
  171.      return False
  172.  return True
  173.  
  174. def vertical_ok(b, z, n):
  175.  o = z%9
  176.  for i in range(9):
  177.    if b[o+i*9] == n:
  178.      return False
  179.  return True
  180.  
  181. def inblock_ok(b, z, n):
  182.  x, y = (z//3)%3, (z//27)
  183.  o = x*3+y*27
  184.  for i in range(9):
  185.    if b[o+rel_off[i]] == n:
  186.      return False
  187.  return True
  188.  
  189. def placable(b, z, n):
  190.  if b[z] != 0: return False
  191.  if not horizontal_ok(b, z, n): return False
  192.  if not vertical_ok(b, z, n): return False
  193.  if not inblock_ok(b, z, n): return False
  194.  return True
  195.  
  196. print_board(b_org)
  197.  
  198. pt = 0
  199.  
  200. def challenge(b, pt):
  201.  while pt <= 80:
  202.    if b[pt] == 0:
  203.      break
  204.    pt += 1
  205.  
  206.  if pt > 80:
  207.    print_board(b)
  208.    print "GOAL"
  209.    return
  210.  
  211.  choices = [i for i in range(1,10) if placable(b, pt, i)]
  212.  for c in choices:
  213.    b[pt] = c
  214.    challenge(b, pt+1)
  215.  b[pt] = 0
  216.  return
  217.  
  218.  
  219. challenge(b, 0)
  220.  
  221. """
  222.  
  223.  
  224. """
  225.  
  226.  
  227.  
  228. def get_neighbours(z):
  229.  rel_off = [0, 1, 2, 9, 10, 11, 18, 19, 20]
  230.  n = set()
  231.  #horizontal
  232.  o = (z//9)*9
  233.  for i in range(9):
  234.    n.add(o+i)
  235.  #vertical
  236.  o = z%9
  237.  for i in range(9):
  238.    n.add(o+i*9)
  239.  #in_block
  240.  x, y = (z//3)%3, (z//27)
  241.  o = x*3+y*27
  242.  for i in range(9):
  243.    n.add(o+rel_off[i])
  244.  n.remove(z)
  245.  return n
  246.  
  247. def placable(b, z, n):
  248.  if b[z] != 0: return False
  249.  for zz in neighbour_table[z]:
  250.    if b[zz] == n: return False
  251.  return True
  252.  
  253. def possible_list(b, z):
  254.  return  [i for i in range(1,10) if placable(b, z, i)]
  255.  
  256. neighbour_table = {}
  257. for i in range(81):
  258.  neighbour_table[i] = get_neighbours(i)
  259.  
  260.  
  261.  
  262. def getprint_boardlacables_and_choices(b):
  263.  _t = []
  264.  for i in range(81):
  265.    l = possible_list(b, i)
  266.    if len(l) > 0:
  267.      _t.append((len(l), i, possible_list(b, i)))
  268.  _t.sort()
  269.  
  270.  placables = [i[1] for i in _t]
  271.  choices = {}
  272.  for i in _t:
  273.    choices[i[1]] = i[2]
  274.  
  275.  return placables, choices
  276.  
  277.  
  278.  
  279. while 1:
  280.  
  281.  placables, choices = getprint_boardlacables_and_choices(b)
  282.  
  283.  t = placables[0]
  284.  
  285.  if len(choices[t]) == 1:
  286.    print "place %d in %d for sure" % (choices[t][0], t)
  287.    b[t] = choices[t][0]
  288.    placables, choices = getprint_boardlacables_and_choices(b)
  289.    continue
  290.  
  291.  print "undecidable. try if placing something detarmines something else.."
  292.  print len(placables)
  293.  for pl in placables:
  294.    for pn in choices[pl]:
  295.      #print pl, pn
  296.      b2 = list(b)
  297.      b2[pl] = pn
  298.      placables2, choices2= getprint_boardlacables_and_choices(b2)
  299.      #print len(placables2)
  300.  break
  301.  
  302. print_board(b_org)
  303. print_board(b)
  304.  
  305. for p in [57, 58, 59, 66, 76, 77]:
  306.  print p, choices[p]
  307. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement