Advertisement
Guest User

Untitled

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