#include <string>
#include <iostream>
#include <vector>
using namespace std;
vector<int> path;
int cnt = 0;
bool isvalid(int col) {
int row = path.size();
for (int i = 0; i < row; i++) {
if (path[i] == col)
return false;
if (abs(i - row) == abs(path[i] - col))
return false;
}
return true;
}
void print(int n) {
cnt++;
cout << cnt<<":";
for (int i = 0; i < n; i++) {
cout << "(" << i+1 << "," << path[i]+1 << ")" << " ";
}
cout << endl;
}
void place(int row, int n) {
if (row >= n)
print(n);
else {
for (int col = 0; col < n; col++)
if (isvalid(col)) {
path.push_back(col);
place(row + 1, n);
path.pop_back();
}
}
}
int main() {
place(0, 4);
return 0;
}
八皇后问题
最新推荐文章于 2026-07-03 10:00:25 发布
由于博客内容为空,暂无法提供包含关键信息的摘要。
1万+

被折叠的 条评论
为什么被折叠?



