live enlargened grid calculation
This commit is contained in:
parent
9cdcdc1208
commit
8fe19e4643
@ -10,26 +10,17 @@ for line in io.lines(arg[3]) do
|
|||||||
end
|
end
|
||||||
table.insert(grid, row)
|
table.insert(grid, row)
|
||||||
end
|
end
|
||||||
|
|
||||||
function printgrid()
|
|
||||||
for r, row in pairs(grid) do
|
|
||||||
for c, cell in pairs(row) do
|
|
||||||
io.write(string.format("%3d", cell))
|
|
||||||
end
|
|
||||||
io.write('\n')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local h, w = #grid, #grid[1]
|
local h, w = #grid, #grid[1]
|
||||||
for r0 = 0, 4 do
|
|
||||||
for c0 = 0, 4 do
|
local function cost(r, c)
|
||||||
for r = 1, h do
|
if r <= 0 or c <= 0 or r > 5 * h or c > 5 * w then
|
||||||
for c = 1, w do
|
return math.huge
|
||||||
local row = grid[r0*h + r] or {}
|
else
|
||||||
row[c0*w + c] = (grid[r][c] + r0 + c0 - 1) % 9 + 1
|
return (grid[(r-1)%h+1][(c-1)%w+1]
|
||||||
grid[r0*h + r] = row
|
+ math.floor((r-1)/h)
|
||||||
end
|
+ math.floor((c-1)/w)
|
||||||
end
|
- 1
|
||||||
|
) % 9 + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,22 +30,21 @@ end)
|
|||||||
|
|
||||||
local distances = {}
|
local distances = {}
|
||||||
|
|
||||||
local H = math.huge
|
|
||||||
local n = { 1, 1, 0 }
|
local n = { 1, 1, 0 }
|
||||||
while n ~= nil and (distances[#grid] == nil or distances[#grid][#grid[1]] == nil) do
|
while n ~= nil and (distances[5*h] == nil or distances[5*h][5*w] == nil) do
|
||||||
local r, c, d = unpack(n)
|
local r, c, d = unpack(n)
|
||||||
if 0 < r or r <= #grid or 0 < c or c <= #grid[r] then
|
if cost(r, c) < math.huge then
|
||||||
local row = distances[r] or {}
|
local row = distances[r] or {}
|
||||||
if d < (row[c] or H) then
|
if d < (row[c] or math.huge) then
|
||||||
row[c] = d
|
row[c] = d
|
||||||
distances[r] = row
|
distances[r] = row
|
||||||
pq:add({ r-1, c, d + ((grid[r-1] or {})[c] or H) })
|
pq:add({ r-1, c, d + cost(r-1, c) })
|
||||||
pq:add({ r, c-1, d + ((grid[r] or {})[c-1] or H) })
|
pq:add({ r, c-1, d + cost(r, c-1) })
|
||||||
pq:add({ r+1, c, d + ((grid[r+1] or {})[c] or H) })
|
pq:add({ r+1, c, d + cost(r+1, c) })
|
||||||
pq:add({ r, c+1, d + ((grid[r] or {})[c+1] or H) })
|
pq:add({ r, c+1, d + cost(r, c+1) })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
n = pq:pop()
|
n = pq:pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
print(distances[#grid] and distances[#grid][#grid[1]])
|
print(distances[5*h] and distances[5*h][5*w])
|
||||||
|
Loading…
Reference in New Issue
Block a user