8queen(稍后补)

来源:互联网 发布:日有所思夜有所梦 知乎 编辑:程序博客网 时间:2024/05/09 05:02

真tm恐怖,居然有人搞出来了:
https://www.zhihu.com/question/263696894/answer/273055085?utm_source=qq&utm_medium=social
……………………………………………………………………………………….
想着可能有O(1)算法,但感觉过于困难,果然是代数功力不够深厚么
……………………………………………………………………………………….
等有空了研究一下置换群
感觉可以直接给出序列表示
下面是暴力

#include <iostream>#include <iterator>using namespace std;ostream_iterator<int> _oit(cout, " ");int _count = 0;bool judge(int* p, int pos, int m) {    for (int i = 0; i <= pos; ++i)        if (p[i] == m || pos + 1 - i == abs(m - p[i]))return false;    return true;}void EightQueens(int* p, int pos) {    if (pos == 7) {        _count++;        cout << _count << ": ";        copy(p, p + 8, _oit); cout << endl;        return;    }    for (int i = 0; i < 8; ++i) {        if (judge(p, pos, i)) {            int _pos = pos;            p[++_pos] = i;            EightQueens(p, _pos);        }    }}int main() {    int queen[8];    memset(queen, 0, sizeof(int) * 8);    EightQueens(queen, -1);    system("pause");    return 0;}
原创粉丝点击