Skip to content

Instantly share code, notes, and snippets.

@rangercyh
Created December 5, 2013 03:40
Show Gist options
  • Save rangercyh/7799752 to your computer and use it in GitHub Desktop.
Save rangercyh/7799752 to your computer and use it in GitHub Desktop.
generate team from multi small group
g_requireTeamNum = 6
g_teamNumLowLimit = 3
g_teamNumHighLimit = 7
--[[
[1] = {
["teamLink"] = {},
["totalNum"] = 4,
["state"] = 1, --1代表不需要匹配,其他为需要匹配
}
]]
local readyTeamList = {}
local inputTeamList = {}
local preTeamList = {}
function addIntoReadyList(nPos, tbAddTeamID)
for _, v in pairs(tbAddTeamID) do
table.insert(readyTeamList[nPos].teamLink, v)
readyTeamList[nPos].totalNum = readyTeamList[nPos].totalNum + get_teamNumByID(v)
end
end
function putIntoReadyList(nPos, tbTeamID)
readyTeamList[nPos] = {}
readyTeamList[nPos].teamLink = tbTeamID
readyTeamList[nPos].totalNum = 0
for i = 1, #tbTeamID do
readyTeamList[nPos].totalNum = readyTeamList[nPos].totalNum + get_teamNumByID(tbTeamID[i])
end
if readyTeamList[nPos].totalNum > g_teamNumLowLimit and
readyTeamList[nPos].totalNum < g_teamNumHighLimit then
readyTeamList[nPos].nState = 1
end
end
function get_teamNumByID(nTeamID)
return inputTeamList[nTeamID]
end
function checkIfComplete(nPos, nTeamNum, nTeamID)
if (readyTeamList[nPos].totalNum + nTeamNum < 7) and
(readyTeamList[nPos].totalNum + nTeamNum > 3) then
table.insert(readyTeamList[nPos].teamLink, nTeamID)
readyTeamList[nPos].totalNum = readyTeamList[nPos].totalNum + nTeamNum
readyTeamList[nPos].nState = 1
return true
else
return false
end
end
function add_team(nTeamNum)
inputTeamList[#inputTeamList + 1] = nTeamNum
--插入准备队列
for i = 1, g_requireTeamNum do
if not(readyTeamList[i]) then
putIntoReadyList(i, { #inputTeamList })
return
else
if readyTeamList[i].nState ~= 1 then
if checkIfComplete(i, nTeamNum, #inputTeamList) then
return
end
end
end
end
--插入分配队列
preTeamList[#preTeamList + 1] = #inputTeamList
end
function show_result()
preTeamListProcess()
print("**************进入队列组合******************")
print("总计", #inputTeamList, "支队伍")
print("输入队列顺序:", table.concat(inputTeamList, ", "))
for i = 1, #readyTeamList do
if readyTeamList[i].nState == 1 then
print(string.format("第%d队ID:%s", i, table.concat(readyTeamList[i].teamLink, ", ")))
print("人数分别为:")
for k, v in ipairs(readyTeamList[i].teamLink) do
print(get_teamNumByID(v))
end
end
end
end
function readyListProc()
--计算空洞位置
local tbProcPos = {}
for i = 1, #readyTeamList do
if readyTeamList[i].nState ~= 1 then
table.insert(tbProcPos, i)
end
end
--如果两两合并能成就两两合并空洞
for i = #tbProcPos, 1, -1 do
for j = 1, i - 1 do
if (readyTeamList[tbProcPos[i]].nState ~= 1) and
(readyTeamList[tbProcPos[i]].totalNum + readyTeamList[tbProcPos[j]].totalNum < g_teamNumHighLimit) then
table.insert(readyTeamList[tbProcPos[j]].teamLink, readyTeamList[tbProcPos[i]].teamLink[1])
readyTeamList[tbProcPos[j]].totalNum = readyTeamList[tbProcPos[j]].totalNum +
readyTeamList[tbProcPos[i]].totalNum
if readyTeamList[tbProcPos[j]].totalNum > g_teamNumLowLimit then
readyTeamList[tbProcPos[j]].nState = 1
end
for k = tbProcPos[i], #readyTeamList - 1 do
readyTeamList[k] = readyTeamList[k + 1]
end
readyTeamList[#readyTeamList] = nil
break
end
end
end
--解除空洞占位
for i = 1, g_requireTeamNum do
if readyTeamList[i] and readyTeamList[i].nState ~= 1 then
table.insert(preTeamList, readyTeamList[i].teamLink[1])
readyTeamList[i] = nil
end
end
end
function preListProc()
--生成候补队伍数量统计表
local tbNumTeam = {}
for k, v in ipairs(preTeamList) do
local nNum = get_teamNumByID(v)
if not(tbNumTeam[nNum]) then
tbNumTeam[nNum] = {}
end
table.insert(tbNumTeam[nNum], v)
end
--生成空位队伍
for i = 1, g_requireTeamNum do
if not(readyTeamList[i]) then
if not(formTeam(i, tbNumTeam)) then
break
end
end
end
return tbNumTeam
end
function formTeam(nPos, tbNumTeam)
for k,v in pairs(tbNumTeam) do print(k, table.concat(v, ",")) end
--6,5,4直接生成
for k = g_teamNumHighLimit - 1, g_teamNumLowLimit + 1 do
if tbNumTeam[k] then
putIntoReadyList(nPos, { tbNumTeam[k][1] })
table.remove(tbNumTeam[k], 1)
return true
end
end
--3 + 3
if tbNumTeam[3] and #tbNumTeam[3] >= 2 then
putIntoReadyList(nPos, { tbNumTeam[3][1], tbNumTeam[3][2] })
print("3+3 = ", tbNumTeam[3][1], tbNumTeam[3][2])
table.remove(tbNumTeam[3], 1)
table.remove(tbNumTeam[3], 1)
return true
end
--3 + 2 or 3 + 1
if tbNumTeam[3] and #tbNumTeam[3] >= 1 and ((#tbNumTeam[2] >= 1) or (#tbNumTeam[1] >= 1)) then
if tbNumTeam[2][1] then
print("3+2 = ", tbNumTeam[3][1], tbNumTeam[2][1])
putIntoReadyList(nPos, { tbNumTeam[3][1], tbNumTeam[2][1] })
table.remove(tbNumTeam[2], 1)
else
print("3+1 = ", tbNumTeam[3][1], tbNumTeam[1][1])
putIntoReadyList(nPos, { tbNumTeam[3][1], tbNumTeam[1][1] })
table.remove(tbNumTeam[1], 1)
end
table.remove(tbNumTeam[3], 1)
return true
end
--2 + 2
if tbNumTeam[2] and #tbNumTeam[2] >= 2 then
print("2+2 = ", tbNumTeam[2][1], tbNumTeam[2][2])
putIntoReadyList(nPos, { tbNumTeam[2][1], tbNumTeam[2][2] })
table.remove(tbNumTeam[2], 1)
table.remove(tbNumTeam[2], 1)
return true
end
--2 + 1 + 1
if tbNumTeam[2] and #tbNumTeam[2] >= 1 and tbNumTeam[1] and #tbNumTeam[1] >= 2 then
print("2+1+1 = ", tbNumTeam[2][1], tbNumTeam[1][1], tbNumTeam[1][2])
putIntoReadyList(nPos, { tbNumTeam[2][1], tbNumTeam[1][1], tbNumTeam[1][2] })
table.remove(tbNumTeam[2], 1)
table.remove(tbNumTeam[1], 1)
table.remove(tbNumTeam[1], 1)
return true
end
--1 + 1 + 1 + 1
if tbNumTeam[1] and #tbNumTeam[1] >= 4 then
print("1+1+1+1 = ", tbNumTeam[1][1], tbNumTeam[1][2], tbNumTeam[1][3], tbNumTeam[1][4])
putIntoReadyList(nPos, { tbNumTeam[1][1], tbNumTeam[1][2], tbNumTeam[1][3], tbNumTeam[1][4]})
table.remove(tbNumTeam[1], 1)
table.remove(tbNumTeam[1], 1)
table.remove(tbNumTeam[1], 1)
table.remove(tbNumTeam[1], 1)
return true
end
return false
end
function preTeamListProcess()
readyListProc()
local tbNumTeam = preListProc()
lastListProc(tbNumTeam)
end
function lastListProc(tbNumTeam)
--填补已成队伍空缺
for i = 1, g_requireTeamNum do
if readyTeamList[i] then
local nHole = g_teamNumHighLimit - readyTeamList[i].totalNum - 1
if (readyTeamList[i].nState == 1) and (nHole > 0) then
if tbNumTeam[2] and nHole == 2 then
addIntoReadyList(i, { tbNumTeam[2][1] })
table.remove(tbNumTeam[2], 1)
else
for j = 1, nHole do
if tbNumTeam[1] then
addIntoReadyList(i, { tbNumTeam[1][1] })
table.remove(tbNumTeam[1], 1)
end
end
end
end
end
end
end
function main()
while (1) do
--只有nNum满足条件,nState才有效果,否则都认为需要匹配
--但是存在满足条件不让匹配又不是在先来队列里的
--所以这里全部认为需要匹配
--local nNum, nState = io.read("*number", "*number")
local nNum = io.read("*number")
if nNum == 0 then
show_result()
return
elseif nNum > 0 and nNum < 7 then
add_team(nNum)
else
print("Wrong Input!")
end
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment