非常经典的n皇后问题
记得在状压DP中有提到用二进制优化的方法
递归:
#include<bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x){
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+ch-'0';
x*=f;
}
int n;
bool heng[311],zong[311],l_t[611],r_t[611];
int a[311];
void dfs(int k){
if(k==n+1){
for(int i=1;i<=n;++i) cout<<a[i]<<" ";
cout<<endl;
}
else{
for(int j=1;j<=n;++j){
if(!heng[k]&&!zong[j]&&!l_t[k+j-1]&&!r_t[n-k+j]){
a[k]=j;
heng[k]=zong[j]=l_t[k+j-1