N-queen
来源:互联网 发布:淘宝网店如何引流 编辑:程序博客网 时间:2024/04/27 23:09
N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
N-queen用了回溯的写法,略区别于迭代。
转自:http://blog.csdn.net/zhangchao3322218/article/details/8091047
学习了回溯的写法。
/*
回溯法有“通用解题法”之称,可以系统的搜索问题的所有解,既有系统性也有跳跃性。
它在问题的解空间树中,按深度优先策略,从根节点出发搜索解空间树。算法搜索至洁空间树
的任一结点时,先判断该结点是否包括该问题的解,如果肯定不包含,则跳过对以该结点为
根的子树的搜索,逐层向其祖先结点回溯;否则进入该子树,继续按深度优先策略搜索。
回溯法解题的三个步骤:
(1)针对所给问题,定义问题的解空间;
(2)确定易于搜索的解空间结构;
(3)以深度优先方式搜索解空间,并在搜索过程中用剪枝函数避免无效搜索。
递归法实现回溯:
其中t表示递归深度,n用来控制递归深度。当t>n时,算法以搜索至叶结点。此时,由output(x)记录或输出
得到的可行解x。for循环中的f(n,t)和g(n,t)分别表示在当前扩展结点处未搜索过的子树的起始编号和终止编号。
h(i)表示在当前扩展结点处x[t]的第i个可选值。constraint(t)和bound(t)是当前扩展结点处的约束函数和
限界函数。
调用一次backtrack(1)即可完成整个回溯搜索过程。
void backtrack(int t)
{
if(t>n)
output(x);
else
{
for(i=f(n,t); i<=g(n,t); i++)
{
x[t]=h(i);
if(constraint(t)&&bound(t))
backtrack(t+1);
}
}
}
采用树的非递归深度优先遍历算法,可将回溯法表示为一个非递归迭代过程
void iterativeBack()
{
int t=1;
while (t>0)
{
if(f(n,t)<g(n,t))
for(i=f(n,t); i<=g(n,t); i++)
{
x[t]=h(i);
if(constraint(t)&&bound(t))
{
if (solution(t))
output(x);
else t++;
}
else t--;
}
}
}
在回溯法的搜索过程中动态产生问题的解空间,在任何时刻,算法只保存从根结点到当前扩展结点
的路径。如果解空间树中从根结点到叶结点的最长路径长度为h(n),则回溯法所需的计算空间为O(h(n)).
而显示地存储整个解空间则需要O(2^h(n))或O(h(n)!)内存空间
————————————————————————————
当所给的问题是从n个元素的集合S中找出S满足某种性质的子集时,相应的解空间树为自己树。O(2^n)
如0-1背包问题,装载问题,符号三角形问题,最大团问题,图的m着色问题,n皇后问题
当所给问题是确定n个元素满足某种性质的排列是,相应的解空间树称为排列树。O(n!)
如批处理作业调度,旅行售货员问题,圆排列问题,电路板排列问题
N-queen的实现:
#include <iostream>#include <cmath>#include<vector>#include<cstdlib>#include<string>using namespace std;class Solution {public: vector<vector<string> > solveNQueens(int n) { numofanswer = 0; numofqueen = n; queen = (int*)malloc(n*sizeof(int)); backtrack(0);free(queen); return ret; }private: int numofanswer; int numofqueen; int *queen; vector<vector<string> >ret; vector<string> vstr; bool place(int k) { for (int j=0; j<k; j++) if((fabs(k-j)==fabs(queen[j]-queen[k]))||(queen[j]==queen[k])) return false; return true; } void backtrack(int t) { int i=0; if(t>numofqueen-1) { numofanswer++; string str; vstr.clear(); for(int j=0;j<numofqueen;j++) str.push_back('.'); for(int j=0;j<numofqueen;j++) vstr.push_back(str); for(int j=0;j<numofqueen;j++) vstr[j][queen[j]]='Q'; ret.push_back(vstr); } else { for(i=0; i<numofqueen; i++) { queen[t]=i; if(place(t)) backtrack(t+1); } } }};int main(){ int m1=4; Solution so; vector<vector<string> > ret; ret = so.solveNQueens(m1); cout<<ret.size()<<endl; cout<<ret[0][0]<<endl; cout<<ret[0][1]<<endl; cout<<ret[0][2]<<endl; cout<<ret[0][3]<<endl; cout<<endl; cout<<ret[1][0]<<endl; cout<<ret[1][1]<<endl; cout<<ret[1][2]<<endl; cout<<ret[1][3]<<endl;return 0;}
另附非递归写法:
http://blog.csdn.net/zhangchao3322218/article/details/8097190
- N queen
- N-queen
- N-Queen
- N-Queen
- N-Queen算法
- [LeetCode] N-Queen
- N-Queen Problem
- [LeetCode] N-Queen N皇后
- Leetcode: N Queen I & II
- Leetcode-"N-Queen 1, 2"
- AI-N Queen Problem(dfs)
- AI-N Queen Problem(bfs)
- Leetcode: N Queen I & II
- N-Queen(java实现)
- lightoj 1061 - N Queen Again
- lintcode-N-Queen, N皇后问题
- The BackTracking algorithm for n queen problem
- Utilizing symmetry characteristic of n queen problem
- 接上一篇
- 如何去处理好你的简历
- OCP 1Z0 051 99
- ps aux授命詳解
- 74LS160 笔记 数电实验
- N-queen
- IOS面试题
- 好的网站
- C语言中的typedef
- 【远控编写01】客户端界面的设计和编写--对话框,列表控件的伸缩
- Writing shellcode in C++
- 【高手】小字节序转为大字节序函数
- linux c语言的split函数和空格处理函数
- java中的数据结构 Collection & Map