#include <iostream>
#include<algorithm>
using namespace std;
string s[1000000];
int num[9] = {1,2,3,4,5,6,7,8,9};
int main()
{
int x;
cin >> x;
if (x == 1)
{
cout << "Q" << endl;
}
else
{
int e = 0;
do
{
for (int i = 0; i < x; i++)
{
s[e] += (num[i] + '0');
}
e++;
} while (next_permutation(num, num + x));
int n = 0;
for (int i = e - 1; i >= 0; i--)
{
bool b = true;
for (int y = 0; y < x - 1; y++)
{
for (int z = y + 1; z < x; z++)
{
if (int(s[i][y]) == int(s[i][z]) + (z-y))
{
b = false;
break;
}
else if (int(s[i][y]) == int(s[i][z]) - (z-y))
{
b = false;
break;
}
}
if (!b) break;
}
if (b)
{
for (int t = 0; t < x; t++)
{
for (int y = 0; y < x; y++)
{
if (y == int(s[i][t]) - 49)
{
cout << "Q";
}
else
{
cout << ".";
}
}
cout << endl;
}
cout << endl;
}
}
}
}
八皇后问题——全排列解法
转载
真好,它们可以飞翔
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:132
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
经典问题“八皇后”的解法
经典问题“八皇后”的经典解法
C语言 八皇后 回溯算法 -
经典算法问题:八皇后的Python解法
想了想,没啥说的,代码不算难,涉及到的知识点本文都有说明:#八皇后问题board=[ [0,0
Python range enumerate 八皇后问题 python