八皇后问题解法(Lua版)
I’m learning Lua programming through reading “Programming in Lua”(PIL).
Just show the demo code in the book and add some debug information to show the program how to run.
- 源代码
- 遍历回溯过程
源代码
local N = 8
local function printsolution(a)
for i = 1, N do
for j = 1, N do
io.write(a[i] == j and "X" or "-", " ")
end
io.write("\n")
end
print("---------------")
end
local function isplace(a, n, c)
for i = 1, n - 1 do
if (a[i] == c) or
(a[i] == (c + (n - i))) or -- a[i] > c
((a[i] + (n - i)) == c) then -- a[i] < c
return false
end
end
return true
end
local solution_count = 0
local function addqueen(a, n)
if n > N then
solution_count = solution_count + 1
---[[
print("solution_count: ", solution_count)
printsolution(a)
--]]
return
end
for c = 1, N do
if isplace(a, n, c) then
print("[" .. n .. "," .. c .. "]")
a[n] = c
addqueen(a, n+1)
end
end
print("stop at row", n)
end
addqueen({}, 1)
遍历回溯过程
Finally, it gets 92 solutions.
>lua -e "io.stdout:setvbuf 'no'" "eight_keen.lua"
[1,1]
[2,3]
[3,5]
[4,2]
[5,4]
stop at row 6
[5,8]
stop at row 6
stop at row 5
[4,7]
[5,2]
[6,4]
[7,6]
stop at row 8
stop at row 7
stop at row 6
[5,4]
stop at row 6
stop at row 5
[4,8]
[5,2]
[6,4]
[7,6]
stop at row 8
stop at row 7
stop at row 6
[5,4]
stop at row 6
stop at row 5
stop at row 4
[3,6]
[4,2]
[5,7]
[6,5]
stop at row 7
stop at row 6
stop at row 5
[4,8]
[5,2]
[6,4]
stop at row 7
[6,5]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,7]
[4,2]
[5,4]
[6,8]
stop at row 7
stop at row 6
[5,8]
[6,5]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,8]
[4,2]
[5,4]
stop at row 6
[5,7]
stop at row 6
stop at row 5
[4,6]
[5,2]
stop at row 6
[5,4]
[6,2]
[7,5]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
stop at row 4
stop at row 3
[2,4]
[3,2]
[4,5]
[5,3]
stop at row 6
[5,8]
stop at row 6
stop at row 5
[4,7]
[5,3]
stop at row 6
stop at row 5
[4,8]
[5,3]
[6,7]
stop at row 7
stop at row 6
[5,6]
[6,3]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,6]
[4,3]
stop at row 5
[4,8]
[5,2]
[6,5]
[7,3]
stop at row 8
stop at row 7
[6,7]
[7,3]
stop at row 8
stop at row 7
stop at row 6
[5,3]
[6,5]
stop at row 7
[6,7]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,7]
[4,3]
[5,6]
[6,2]
[7,5]
stop at row 8
stop at row 7
stop at row 6
[5,8]
[6,2]
[7,5]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,5]
[5,2]
stop at row 6
[5,3]
stop at row 6
[5,8]
[6,2]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,8]
[4,3]
stop at row 5
[4,5]
[5,2]
stop at row 6
[5,3]
stop at row 6
stop at row 5
stop at row 4
stop at row 3
[2,5]
[3,2]
[4,6]
[5,3]
[6,7]
[7,4]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,8]
[5,3]
[6,7]
[7,4]
stop at row 8
stop at row 7
stop at row 6
[5,6]
[6,3]
stop at row 7
[6,4]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,7]
[4,2]
[5,4]
[6,8]
stop at row 7
stop at row 6
[5,6]
[6,3]
stop at row 7
[6,8]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,8]
[4,2]
[5,4]
[6,7]
[7,3]
stop at row 8
stop at row 7
stop at row 6
[5,7]
[6,3]
[7,6]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,6]
[5,3]
[6,7]
[7,2]
[8,4]
solution_count: 1
X - - - - - - -
- - - - X - - -
- - - - - - - X
- - - - - X - -
- - X - - - - -
- - - - - - X -
- X - - - - - -
- - - X - - - -
---------------
..... (中间省略90种解法)
---------------
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,7]
[5,2]
[6,6]
stop at row 7
stop at row 6
[5,5]
[6,2]
[7,6]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,5]
[4,2]
stop at row 5
[4,7]
[5,1]
[6,4]
[7,6]
stop at row 8
stop at row 7
[6,6]
stop at row 7
stop at row 6
[5,2]
[6,4]
[7,6]
stop at row 8
stop at row 7
[6,6]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,7]
[4,2]
stop at row 5
[4,4]
[5,1]
[6,5]
stop at row 7
stop at row 6
[5,2]
[6,5]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
stop at row 3
[2,4]
[3,1]
[4,3]
[5,5]
[6,2]
stop at row 7
[6,7]
stop at row 7
stop at row 6
[5,6]
[6,2]
[7,7]
[8,5]
solution_count: 92
- - - - - - - X
- - - X - - - -
X - - - - - - -
- - X - - - - -
- - - - - X - -
- X - - - - - -
- - - - - - X -
- - - - X - - -
---------------
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,7]
[5,2]
[6,6]
[7,3]
stop at row 8
stop at row 7
stop at row 6
[5,5]
[6,2]
[7,6]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,2]
[4,7]
[5,3]
[6,1]
stop at row 7
[6,6]
stop at row 7
stop at row 6
[5,5]
[6,1]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,7]
[4,1]
[5,3]
[6,5]
stop at row 7
[6,6]
stop at row 7
stop at row 6
[5,6]
[6,2]
[7,5]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,3]
[5,6]
[6,2]
[7,5]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
stop at row 4
stop at row 3
[2,5]
[3,1]
[4,4]
[5,6]
stop at row 6
[5,7]
stop at row 6
stop at row 5
[4,6]
stop at row 5
stop at row 4
[3,2]
[4,4]
[5,1]
[6,7]
stop at row 7
stop at row 6
[5,6]
stop at row 6
[5,7]
stop at row 6
stop at row 5
[4,6]
[5,1]
[6,7]
[7,4]
stop at row 8
stop at row 7
stop at row 6
[5,3]
[6,7]
[7,4]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,3]
[4,1]
[5,6]
[6,2]
stop at row 7
[6,4]
stop at row 7
stop at row 6
[5,7]
[6,2]
[7,6]
stop at row 8
stop at row 7
[6,4]
[7,6]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,6]
stop at row 5
stop at row 4
[3,7]
[4,1]
[5,3]
[6,6]
stop at row 7
stop at row 6
[5,6]
[6,2]
stop at row 7
stop at row 6
stop at row 5
[4,2]
[5,6]
stop at row 6
stop at row 5
[4,4]
[5,1]
stop at row 6
[5,6]
stop at row 6
stop at row 5
stop at row 4
stop at row 3
[2,6]
[3,1]
[4,3]
[5,5]
[6,7]
[7,4]
stop at row 8
stop at row 7
stop at row 6
[5,7]
stop at row 6
stop at row 5
[4,7]
[5,2]
stop at row 6
[5,5]
stop at row 6
stop at row 5
stop at row 4
[3,2]
[4,7]
[5,1]
[6,4]
stop at row 7
stop at row 6
[5,5]
[6,1]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,3]
[4,1]
[5,7]
[6,4]
stop at row 7
[6,5]
stop at row 7
stop at row 6
stop at row 5
[4,7]
[5,2]
[6,4]
stop at row 7
stop at row 6
stop at row 5
stop at row 4
[3,4]
[4,1]
[5,5]
stop at row 6
[5,7]
[6,5]
[7,3]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,2]
[5,5]
stop at row 6
[5,7]
[6,5]
[7,3]
stop at row 8
stop at row 7
stop at row 6
stop at row 5
[4,7]
[5,1]
stop at row 6
[5,5]
stop at row 6
stop at row 5
stop at row 4
stop at row 3
stop at row 2
stop at row 1
>Exit code: 0