八皇后问题解法(Lua版)
来源:互联网 发布:大宗商品网络交易平台 编辑:程序博客网 时间:2024/05/18 04:00
八皇后问题解法(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 = 8local 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("---------------")endlocal 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 trueendlocal solution_count = 0local 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)endaddqueen({}, 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 6stop at row 5[4,7][5,2][6,4][7,6]stop at row 8stop at row 7stop at row 6[5,4]stop at row 6stop at row 5[4,8][5,2][6,4][7,6]stop at row 8stop at row 7stop at row 6[5,4]stop at row 6stop at row 5stop at row 4[3,6][4,2][5,7][6,5]stop at row 7stop at row 6stop at row 5[4,8][5,2][6,4]stop at row 7[6,5]stop at row 7stop at row 6stop at row 5stop at row 4[3,7][4,2][5,4][6,8]stop at row 7stop at row 6[5,8][6,5]stop at row 7stop at row 6stop at row 5stop at row 4[3,8][4,2][5,4]stop at row 6[5,7]stop at row 6stop at row 5[4,6][5,2]stop at row 6[5,4][6,2][7,5]stop at row 8stop at row 7stop at row 6stop at row 5stop at row 4stop at row 3[2,4][3,2][4,5][5,3]stop at row 6[5,8]stop at row 6stop at row 5[4,7][5,3]stop at row 6stop at row 5[4,8][5,3][6,7]stop at row 7stop at row 6[5,6][6,3]stop at row 7stop at row 6stop at row 5stop at row 4[3,6][4,3]stop at row 5[4,8][5,2][6,5][7,3]stop at row 8stop at row 7[6,7][7,3]stop at row 8stop at row 7stop at row 6[5,3][6,5]stop at row 7[6,7]stop at row 7stop at row 6stop at row 5stop at row 4[3,7][4,3][5,6][6,2][7,5]stop at row 8stop at row 7stop at row 6[5,8][6,2][7,5]stop at row 8stop at row 7stop at row 6stop at row 5[4,5][5,2]stop at row 6[5,3]stop at row 6[5,8][6,2]stop at row 7stop at row 6stop at row 5stop at row 4[3,8][4,3]stop at row 5[4,5][5,2]stop at row 6[5,3]stop at row 6stop at row 5stop at row 4stop at row 3[2,5][3,2][4,6][5,3][6,7][7,4]stop at row 8stop at row 7stop at row 6stop at row 5[4,8][5,3][6,7][7,4]stop at row 8stop at row 7stop at row 6[5,6][6,3]stop at row 7[6,4]stop at row 7stop at row 6stop at row 5stop at row 4[3,7][4,2][5,4][6,8]stop at row 7stop at row 6[5,6][6,3]stop at row 7[6,8]stop at row 7stop at row 6stop at row 5stop at row 4[3,8][4,2][5,4][6,7][7,3]stop at row 8stop at row 7stop at row 6[5,7][6,3][7,6]stop at row 8stop at row 7stop at row 6stop at row 5[4,6][5,3][6,7][7,2][8,4]solution_count: 1X - - - - - - - - - - - X - - - - - - - - - - X - - - - - X - - - - X - - - - - - - - - - - X - - X - - - - - - - - - X - - - - --------------- ..... (中间省略90种解法)---------------stop at row 8stop at row 7stop at row 6stop at row 5[4,7][5,2][6,6]stop at row 7stop at row 6[5,5][6,2][7,6]stop at row 8stop at row 7stop at row 6stop at row 5stop at row 4[3,5][4,2]stop at row 5[4,7][5,1][6,4][7,6]stop at row 8stop at row 7[6,6]stop at row 7stop at row 6[5,2][6,4][7,6]stop at row 8stop at row 7[6,6]stop at row 7stop at row 6stop at row 5stop at row 4[3,7][4,2]stop at row 5[4,4][5,1][6,5]stop at row 7stop at row 6[5,2][6,5]stop at row 7stop at row 6stop at row 5stop at row 4stop at row 3[2,4][3,1][4,3][5,5][6,2]stop at row 7[6,7]stop at row 7stop 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 8stop at row 7stop at row 6stop at row 5[4,7][5,2][6,6][7,3]stop at row 8stop at row 7stop at row 6[5,5][6,2][7,6]stop at row 8stop at row 7stop at row 6stop at row 5stop at row 4[3,2][4,7][5,3][6,1]stop at row 7[6,6]stop at row 7stop at row 6[5,5][6,1]stop at row 7stop at row 6stop at row 5stop at row 4[3,7][4,1][5,3][6,5]stop at row 7[6,6]stop at row 7stop at row 6[5,6][6,2][7,5]stop at row 8stop at row 7stop at row 6stop at row 5[4,3][5,6][6,2][7,5]stop at row 8stop at row 7stop at row 6stop at row 5stop at row 4stop at row 3[2,5][3,1][4,4][5,6]stop at row 6[5,7]stop at row 6stop at row 5[4,6]stop at row 5stop at row 4[3,2][4,4][5,1][6,7]stop at row 7stop at row 6[5,6]stop at row 6[5,7]stop at row 6stop at row 5[4,6][5,1][6,7][7,4]stop at row 8stop at row 7stop at row 6[5,3][6,7][7,4]stop at row 8stop at row 7stop at row 6stop at row 5stop at row 4[3,3][4,1][5,6][6,2]stop at row 7[6,4]stop at row 7stop at row 6[5,7][6,2][7,6]stop at row 8stop at row 7[6,4][7,6]stop at row 8stop at row 7stop at row 6stop at row 5[4,6]stop at row 5stop at row 4[3,7][4,1][5,3][6,6]stop at row 7stop at row 6[5,6][6,2]stop at row 7stop at row 6stop at row 5[4,2][5,6]stop at row 6stop at row 5[4,4][5,1]stop at row 6[5,6]stop at row 6stop at row 5stop at row 4stop at row 3[2,6][3,1][4,3][5,5][6,7][7,4]stop at row 8stop at row 7stop at row 6[5,7]stop at row 6stop at row 5[4,7][5,2]stop at row 6[5,5]stop at row 6stop at row 5stop at row 4[3,2][4,7][5,1][6,4]stop at row 7stop at row 6[5,5][6,1]stop at row 7stop at row 6stop at row 5stop at row 4[3,3][4,1][5,7][6,4]stop at row 7[6,5]stop at row 7stop at row 6stop at row 5[4,7][5,2][6,4]stop at row 7stop at row 6stop at row 5stop at row 4[3,4][4,1][5,5]stop at row 6[5,7][6,5][7,3]stop at row 8stop at row 7stop at row 6stop at row 5[4,2][5,5]stop at row 6[5,7][6,5][7,3]stop at row 8stop at row 7stop at row 6stop at row 5[4,7][5,1]stop at row 6[5,5]stop at row 6stop at row 5stop at row 4stop at row 3stop at row 2stop at row 1>Exit code: 0
0 0
- 八皇后问题解法(Lua版)
- 八皇后问题解法
- 八皇后问题解法
- 八皇后问题解法
- 八皇后问题解法
- Lua 八皇后问题
- Lua 八皇后问题
- 八皇后问题的解法
- 八皇后问题解法二
- 八皇后问题DFS解法
- 八皇后问题Python解法
- 八皇后问题回溯解法
- 八皇后问题递归解法
- 八皇后问题----另类解法
- 八皇后问题简单解法
- 八皇后问题(递归解法)
- 八皇后问题详细解法
- lua解决八皇后问题
- 如何让PJSIP 支持IPV6
- VOC Segmentation GT图像颜色表生成分析
- HDU 5821 Ball(贪心)
- LinkedBlockingQueue的put,add跟offer的区别
- css选择器——:only-of-type
- 八皇后问题解法(Lua版)
- QTreeWidget学习之拖拽节点后,需要点击两次节点才生效
- ADT下载地址,完整版
- jsp分页总结
- 【1】【匹配】HDU5821 Ball
- Android如何在局域网中发送广播
- Memcache简单使用
- C++学习002-C++代码中插入汇编语句
- 数据挖掘中的矩阵基础(2)