1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<string> 7 #include<cmath> 8 #include<set> 9 #include<vector> 10 #include<stack> 11 #include<queue> 12 #include<map> 13 using namespace std; 14 #define ll long long 15 #define se second 16 #define fi first 17 const int INF= 0x3f3f3f3f; 18 const int N=5e5+5; 19 20 int n,a[200],x1[200],x2[200],y[200],num=0; //x1,x2存的是对角线的情况 21 22 void dfs(int x) 23 { 24 if(x>n){ 25 if(num<3){ 26 for(int i=1;i<=n;i++) 27 cout<<a[i]<<' '; 28 cout<<endl; 29 } 30 num++; 31 return; 32 } 33 for(int i=1;i<=n;i++) 34 { 35 a[x]=i; 36 if( !y[i] && !x1[x+i] && !x2[x-i+n]) 37 { 38 y[i]=1; 39 x1[x+i]=1; 40 x2[x-i+n]=1; 41 dfs(x+1); 42 y[i]=0; 43 x1[x+i]=0; 44 x2[x-i+n]=0; 45 } 46 } 47 } 48 49 int main() 50 { 51 cin>>n; 52 dfs(1); 53 cout<<num; 54 }
八皇后 优化
猜你喜欢
转载自www.cnblogs.com/thunder-110/p/9297614.html
今日推荐
周排行