結果
問題 | No.421 しろくろチョコレート |
ユーザー |
👑 |
提出日時 | 2020-04-26 00:15:07 |
言語 | Lua (LuaJit 2.1.1734355927) |
結果 |
AC
|
実行時間 | 71 ms / 2,000 ms |
コード長 | 3,773 bytes |
コンパイル時間 | 342 ms |
コンパイル使用メモリ | 6,940 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-23 07:25:33 |
合計ジャッジ時間 | 2,330 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 65 |
ソースコード
local WhiteBlack = {}WhiteBlack.create = function(self)self.edge = {}self.invedge = {}self.leftnode = {}self.rightmatched = {}endWhiteBlack.addNode = function(self, node, isLeft)if isLeft thenself.leftnode[node] = trueself.edge[node] = {}elseself.invedge[node] = {}endendWhiteBlack.addEdge = function(self, src, dst)assert(self.leftnode[src])self.edge[src][dst] = 1self.invedge[dst][src] = 0endWhiteBlack.run = function(self, spos)local tasks = {spos}local tasked = {}local dstpos = 0tasked[spos] = truelocal len = {}len[spos] = 0local found = falsewhile 0 < #tasks dolocal src = tasks[#tasks]table.remove(tasks)tasked[src] = falseif self.leftnode[src] thenfor dst, c in pairs(self.edge[src]) doif c == 1 and (not len[dst] or len[src] + 1 < len[dst]) thenlen[dst] = len[src] + 1if not self.rightmatched[dst] thenself.rightmatched[dst] = truefound = truedstpos = dstbreakelseif not tasked[dst] thentable.insert(tasks, dst)tasked[dst] = trueendendendelseif self.invedge[src] thenfor dst, c in pairs(self.invedge[src]) doif c == 1 and (not len[dst] or len[src] + 1 < len[dst]) thenlen[dst] = len[src] + 1if not tasked[dst] thentable.insert(tasks, dst)tasked[dst] = trueendendendendendif found then break endendif found thenlocal l = len[dstpos]for i = l, 1, -1 doif i % 2 == 1 thenfor src, _u in pairs(self.invedge[dstpos]) doif self.edge[src][dstpos] == 1 and len[src] == i - 1 thenself.edge[src][dstpos] = 0self.invedge[dstpos][src] = 1dstpos = srcbreakendendelsefor src, _u in pairs(self.edge[dstpos]) doif self.invedge[src][dstpos] == 1 and len[src] == i - 1 thenself.invedge[src][dstpos] = 0self.edge[dstpos][src] = 1dstpos = srcbreakendendendendreturn trueelse return false endendWhiteBlack.getMatchCount = function(self)local cnt = 0for src, _u in pairs(self.leftnode) dolocal res = self:run(src)if res then cnt = cnt + 1 endendreturn cntendWhiteBlack.new = function()local obj = {}setmetatable(obj, {__index = WhiteBlack})obj:create()return objendlocal h, w = io.read("*n", "*n", "*l")local map = {}local wb = WhiteBlack.new()local wcnt, bcnt = 0, 0for i = 1, h dolocal s = io.read()for j = 1, w dolocal idx = (i - 1) * w + jif s:sub(j, j) == "." thenmap[idx] = falseelsemap[idx] = trueif (i + j) % 2 == 0 thenwcnt = wcnt + 1wb:addNode(idx, true)elsebcnt = bcnt + 1wb:addNode(idx, false)endendendendfor i = 1, h dofor j = 1, w - 1 dolocal idx = (i - 1) * w + jif map[idx] and map[idx + 1] thenif (i + j) % 2 == 0 thenwb:addEdge(idx, idx + 1)elsewb:addEdge(idx + 1, idx)endendendendfor j = 1, w dofor i = 1, h - 1 dolocal idx = (i - 1) * w + jif map[idx] and map[idx + w] thenif (i + j) % 2 == 0 thenwb:addEdge(idx, idx + w)elsewb:addEdge(idx + w, idx)endendendendlocal matched = wb:getMatchCount()wcnt, bcnt = wcnt - matched, bcnt - matchedlocal paired = math.min(wcnt, bcnt)wcnt, bcnt = wcnt - paired, bcnt - pairedprint(matched * 100 + paired * 10 + wcnt + bcnt)