Advertisement
Guest User

Untitled

a guest
Jul 12th, 2021
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. grid = {}
  2.  
  3. function printGrid()
  4.     local function putc(c) io.stdout:write(c) end
  5.     for i=1,9 do
  6.         putc('[')
  7.         putc(grid[i][1] or ' ')
  8.         for j=2,9 do
  9.             putc(',')
  10.             putc(grid[i][j] or ' ')
  11.         end
  12.         putc(']')
  13.         putc('\n')
  14.     end
  15. end
  16.  
  17. function getPosibilities(x,y)
  18.     local num, pos = 9, {true,true,true,true,true,true,true,true,true}
  19.    
  20.     local function chk(i,j)
  21.         local t = grid[i][j]
  22.         if t and pos[t] then num, pos[t] = num-1 end
  23.         return num==0
  24.     end
  25.  
  26.     for z=1,9 do
  27.         if chk(x,z) or chk(z,y) then return num,pos end
  28.     end
  29.    
  30.     local gx = math.ceil(x/3)*3
  31.     local gy = math.ceil(y/3)*3
  32.     for i=gx-2,gx do
  33.         for j=gy-2,gy do
  34.             if chk(i,j) then return num,pos end
  35.         end
  36.     end
  37.    
  38.     return num,pos
  39. end
  40.  
  41. local h  = {0,0,0,0,0,0,0,0,0}
  42. local v  = {0,0,0,0,0,0,0,0,0}
  43. local g  = {0,0,0,0,0,0,0,0,0}
  44. function stat(x,y,delta)
  45.     local k = 3*math.ceil(x/3)+math.ceil(y/3)-3
  46.     h[x],v[y],g[k] = h[x]+delta,v[y]+delta,g[k]+delta
  47. end
  48.  
  49. function prep()
  50.     for i=1,9 do h[i],v[i],g[i] = 0,0,0 end
  51.     for i=1,9 do for j=1,9 do if grid[i][j] then stat(i,j,1) end end end
  52. end
  53.  
  54. function findPossibilities()
  55.     local n,x,y = -1
  56.     for i=1,9 do
  57.         for j=1,9 do if grid[i][j]==nil then
  58.             local num = h[i] + v[j] + g[3*math.ceil(i/3)+math.ceil(j/3)-3]
  59.             if num>n then n,x,y = num,i,j end
  60.         end end
  61.     end
  62.     if n>=0 then
  63.         local num,pos = getPosibilities(x,y)
  64.         return num,pos,x,y
  65.     else
  66.         return 10
  67.     end
  68. end
  69.  
  70.  
  71. local depth,max = 0,0
  72. function recurse()
  73.     depth = depth + 1; if depth>max then max=depth end
  74.     local n,p,x,y = findPossibilities()
  75.     if n<10 then
  76.         stat(x,y,1)
  77.         for v in pairs(p) do
  78.             grid[x][y] = v
  79.             if recurse() then depth=depth-1; return true end
  80.         end
  81.         stat(x,y,-1)
  82.         grid[x][y] = nil
  83.         depth=depth-1
  84.         return false
  85.     else
  86.         print('solution:')
  87.         printGrid()
  88.         depth=depth-1
  89.         return true
  90.     end
  91. end
  92.  
  93. grilles = {
  94.     {
  95.         "000000001",
  96.         "210000904",
  97.         "403500600",
  98.         "900204500",
  99.         "002705800",
  100.         "001608003",
  101.         "004007309",
  102.         "709000068",
  103.         "100000000"
  104.     },{
  105.         "060230000",
  106.         "008004000",
  107.         "305008047",
  108.         "002000001",
  109.         "500060009",
  110.         "900000700",
  111.         "740900506",
  112.         "000400900",
  113.         "000083070"
  114.     },{
  115.         "003850090",
  116.         "000000100",
  117.         "600000027",
  118.         "000100900",
  119.         "060507080",
  120.         "008003000",
  121.         "150000003",
  122.         "002000000",
  123.         "090085400"
  124.     },{
  125.         "075006800",
  126.         "000000003",
  127.         "300510600",
  128.         "020000000",
  129.         "054801290",
  130.         "000000050",
  131.         "002059006",
  132.         "700000000",
  133.         "003100980"
  134.     },{
  135.         "605000040",
  136.         "000002076",
  137.         "000700300",
  138.         "000620080",
  139.         "087050620",
  140.         "000084000",
  141.         "001005000",
  142.         "520800000",
  143.         "060000905"
  144.     },{
  145.         "000360100",
  146.         "000080000",
  147.         "941502000",
  148.         "050008007",
  149.         "060020090",
  150.         "800900050",
  151.         "000106978",
  152.         "000040000",
  153.         "008097000"
  154.     },{
  155.         "970000000",
  156.         "305002940",
  157.         "000095000",
  158.         "000200005",
  159.         "004080200",
  160.         "500009000",
  161.         "000670000",
  162.         "082900703",
  163.         "000000081"
  164.     },{
  165.         "000003009",
  166.         "048006072",
  167.         "100000400",
  168.         "709430000",
  169.         "000020000",
  170.         "000067201",
  171.         "002000007",
  172.         "930700610",
  173.         "400600000"
  174.     },{
  175.         "000100003",
  176.         "000005800",
  177.         "010007694",
  178.         "000058000",
  179.         "054902380",
  180.         "000710000",
  181.         "678300020",
  182.         "003500000",
  183.         "900001000"
  184.     },{
  185.         "000000608",
  186.         "569300000",
  187.         "007600000",
  188.         "100000500",
  189.         "080975020",
  190.         "050000004",
  191.         "000007200",
  192.         "000004753",
  193.         "903000000"
  194.     },{
  195.         "080000103",
  196.         "709000824",
  197.         "005000007",
  198.         "020809000",
  199.         "000401000",
  200.         "000206080",
  201.         "200000500",
  202.         "451000302",
  203.         "807000060"
  204.     },{
  205.         "006007590",
  206.         "000000020",
  207.         "000063100",
  208.         "095870002",
  209.         "000040000",
  210.         "100035960",
  211.         "007380000",
  212.         "040000000",
  213.         "059700600"
  214.     },{
  215.         "003009060",
  216.         "708000409",
  217.         "500000200",
  218.         "001200000",
  219.         "000805000",
  220.         "000006100",
  221.         "005000004",
  222.         "804000307",
  223.         "020500900"
  224.     },{
  225.         "000780000",
  226.         "070000200",
  227.         "010500497",
  228.         "800012500",
  229.         "500000006",
  230.         "006350004",
  231.         "432005060",
  232.         "001000050",
  233.         "000039000"
  234.     },{
  235.         "500007000",
  236.         "000008703",
  237.         "370010002",
  238.         "200000500",
  239.         "005362400",
  240.         "001000008",
  241.         "600090045",
  242.         "104500000",
  243.         "000200009"
  244.     },{
  245.         "000050600",
  246.         "103000700",
  247.         "450300000",
  248.         "020005060",
  249.         "049020180",
  250.         "030100090",
  251.         "000008016",
  252.         "005000903",
  253.         "006030000"
  254.     },{ -- seulement 17 indices ==> dur !
  255.         "000000010",
  256.         "000002003",
  257.         "000400000",
  258.         "000000500",
  259.         "401600000",
  260.         "007100000",
  261.         "050000200",
  262.         "000080040",
  263.         "030910000"
  264.     }
  265. }
  266.  
  267. start = os.clock()
  268. for no,g in ipairs(grilles) do
  269.      for i=1,9 do grid[i] = {} end
  270.  
  271.     for i,s in ipairs(g) do
  272.         for j=1,9 do
  273.             local n = tonumber(s:sub(j,j))
  274.             if n>0 then grid[i][j] = n end
  275.         end
  276.     end
  277.     print('input:', no)
  278.     printGrid()
  279.     max = 0
  280.     local start = os.clock()
  281.     prep()
  282.     if not recurse() then print('no solution!') end
  283.     print('depth:', max)
  284.     print('time:', os.clock()-start,'sec')
  285.     print()
  286. end
  287. print('total time:', os.clock() - start,'sec')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement