45 lines
956 B
Lua
45 lines
956 B
Lua
require "utils"
|
|
|
|
function calValidOptions(b, x, y)
|
|
if b[x][y] ~= 0 then return {} end
|
|
local elm
|
|
local u = {}
|
|
local v = {}
|
|
for i = 1, 9 do
|
|
elm = b[x][i]
|
|
if elm ~= 0 and find(u, elm) == 0 then
|
|
table.insert(u, elm)
|
|
end
|
|
elm = b[i][y]
|
|
if elm ~= 0 and find(u, elm) == 0 then
|
|
table.insert(u, elm)
|
|
end
|
|
end
|
|
local xc = ((x - 1) // 3) * 3
|
|
local yc = ((y - 1) // 3) * 3
|
|
for i = 1, 3 do
|
|
for j = 1, 3 do
|
|
elm = b[i+xc][j+yc]
|
|
if elm ~= 0 and find(u, elm) == 0 then
|
|
table.insert(u, elm)
|
|
end
|
|
end
|
|
end
|
|
for i = 1, 9 do
|
|
if find(u, i) == 0 then table.insert(v, i) end
|
|
end
|
|
return v
|
|
end
|
|
|
|
function buildSearchSpace(b)
|
|
local s = {}
|
|
for i = 1, 9 do
|
|
s[i] = {}
|
|
for j = 1, 9 do
|
|
s[i][j] = calValidOptions(b, i, j)
|
|
end
|
|
end
|
|
return s
|
|
end
|
|
|