Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua gen_table算法写法的修改;生成的tbl排序 #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*.ipch
*.suo
*.log
*.pch
*.pch
*.DS_Store
71 changes: 34 additions & 37 deletions mjlib_lua/base/gen_table.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
local table_mgr = require "table_mgr"
local tconcat = table.concat

local tested_tbl = {}

local function dump_t(t)
return tconcat(t, "")
end

local function add_to_table(t)
local num = 0
for i=1,9 do
num = num * 10 + t[i]
end
local num = tonumber(dump_t(t))

if tested_tbl[num] then
return
end

tested_tbl[num] = true
local key = 0
for i=1,9 do
if t[i] > 0 then
key = key*10 + t[i]
else
if key > 0 then
table_mgr:add(key)
key = 0
end
end
end

if key > 0 then
table_mgr:add(key)
end
string.gsub(num..'', '([1-9]+)', function(key)
table_mgr:add(key)
end)
end

local do_add
local gen_table_sub

do_add = function(t, num)
add_to_table(t)
if num < 4 then
gen_table_sub(t, num + 1)
end
end

local function gen_table_sub(t, num)
gen_table_sub = function(t, num)
for j=1,16 do
repeat
local index
Expand All @@ -39,29 +40,25 @@ local function gen_table_sub(t, num)
break
end
t[j] = t[j] + 3

do_add(t, num)

t[j] = t[j] - 3
elseif j<= 16 then
index = j - 9
if t[index] >= 4 or t[index+1] >= 4 or t[index+2] >= 4 then
break
end
end
if index then
t[index] = t[index] + 1
t[index + 1] = t[index + 1] + 1
t[index + 2] = t[index + 2] + 1
end

add_to_table(t)
if num < 4 then
gen_table_sub(t, num + 1)
end
t[index] = t[index] + 1
t[index + 1] = t[index + 1] + 1
t[index + 2] = t[index + 2] + 1

if j<= 9 then
t[j] = t[j] - 3
else
t[index] = t[index] - 1
t[index + 1] = t[index + 1] - 1
t[index + 2] = t[index + 2] - 1
do_add(t, num)

t[index] = t[index] - 1
t[index + 1] = t[index + 1] - 1
t[index + 2] = t[index + 2] - 1
end
until(true)
end
Expand All @@ -86,8 +83,8 @@ end

local function main()
gen_table()
gen_eye_table()
table_mgr:dump()
gen_eye_table()
table_mgr:dump()
end

main()
9 changes: 7 additions & 2 deletions mjlib_lua/base/table_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ end

function M:_dump(file, tbl)
local f = io.open(file, "w+")
local t = {}
for k,_ in pairs(tbl) do
f:write(k.."\n")
end
table.insert(t, tonumber(k))
end
table.sort(t)
for _, v in ipairs(t) do
f:write(v.."\n")
end
f:close()
end

Expand Down
Loading