Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- S = {}
- vis = {}
- n = 20
- function clone(tab)
- local r = {}
- for k,v in pairs(tab) do r[k] = v end
- return r
- end
- function gen(perm)
- local perms = {}
- for i=1,#perm-1 do
- if perm[i] ~= perm[i+1] then
- local p = clone(perm)
- p[i], p[i+1] = p[i+1], p[i]
- perms[#perms+1] = p
- end
- end
- return perms
- end
- u = {'a', 'a', 'a', 'b', 'b', 'b'}
- function hamilton(v, step)
- if(step==n) then
- S[#S+1] = table.concat(v)
- return true
- end
- vis[table.concat(v)] = true
- for _, w in ipairs(gen(v)) do
- if not vis[table.concat(w)] and hamilton(w, step+1) then
- S[#S+1] = table.concat(v)
- return true
- end
- end
- vis[table.concat(v)] = false
- return false
- end
- hamilton(u, 1)
- for i=#S,1,-1 do print("\""..S[i].."\",") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement