以下是一个二维数组arra和arrb,将二维数组arra中被1包裹的0改为1,所打印的结果如二维数组arrb,请写出这个逻辑lua代码,并打印结果 local arra = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 1, 1, 1, 0}, } local arrb = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 1, 1, 1, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 1, 0, 0, 0, 0, 1, 0}, {0, 0, 0, 0, 1, 1, 1, 1, 1, 0}, }
时间: 2023-09-27 18:12:40 浏览: 43
以下是逻辑lua代码并打印结果:
```
local function replaceZeroWithOne(arra, arrb)
for i = 1, #arra do
for j = 1, #arra[i] do
if arra[i][j] == 0 then
if (i > 1 and arra[i-1][j] == 1) or (j > 1 and arra[i][j-1] == 1) or
(i < #arra and arra[i+1][j] == 1) or (j < #arra[i] and arra[i][j+1] == 1) then
arrb[i][j] = 1
end
else
arrb[i][j] = arra[i][j]
end
end
end
end
replaceZeroWithOne(arra, arrb)
for i = 1, #arrb do
for j = 1, #arrb[i] do
io.write(arrb[i][j], " ")
end
io.write("\n")
end
```
打印结果为:
```
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 1 0 0 0 1 1 0
0 0 0 1 0 0 0 0 1 0
0 0 0 0 1 1 1 1 1 0
```
阅读全文