题解
2024-08-01 21:40:26
发布于:上海
17阅读
0回复
0点赞
#include <iostream>
#include <vector>
using namespace std;
bool board[20][20];
vector<vector<int> > ans;
vector<int> path;
int n;
bool is_valid(int x,int y){
for(int i = 1;i < x;i++){
if(board[i][y]) return false;
if(x - i >= 1 && y - i >= 1 && board[x - i][y - i]) return false;
if(x - i >= 1 && y + i <= n && board[x - i][y + i]) return false;
}
return true;
}
void dfs(int now){
if(now == n + 1){
ans.push_back(path);
return ;
}
for(int i = 1;i <= n;i++){
if(!is_valid(now,i)) continue;
board[now][i] = true;
path.push_back(i);
dfs(now + 1);
board[now][i] = false;
path.pop_back();
}
}
int main(){
cin >> n;
dfs(1);
for(int i = 0;i < 3;i++){
for(int j = 0;j < n;j++) cout << ans[i][j] << " ";
cout << endl;
}
cout << ans.size() << endl;
return 0;
}
这里空空如也
有帮助,赞一个