标签:
6
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 int n; 7 int a[15];//×ÝÁÐ 8 int b[30];//ÐмÓÁеĽá¹ûÖµ 9 int c[30];//ÐмõÁеĽá¹ûÖµ 10 int rec[15];//¼Ç¼×ÝÁеÄÁÐÊý 11 int ans=0; 12 void dfs(int num)//hang 13 { 14 for(int i=1;i<=n;i++) 15 { 16 if(a[i]==0&&b[i+num]==0&&c[num-i+14]==0) 17 { 18 rec[num]=i; 19 if(n==num) 20 { 21 ans++; 22 if(ans<=3) 23 { 24 for(int j=1;j<=n;j++) printf("%d ",rec[j]); 25 puts(""); 26 } 27 continue; 28 } 29 a[i]=b[i+num]=c[num-i+14]=1; 30 dfs(num+1); 31 a[i]=b[i+num]=c[num-i+14]=0; 32 } 33 } 34 } 35 int main() 36 { 37 cin>>n; 38 dfs(1); 39 cout<<ans; 40 puts(""); 41 return 0; 42 }
标签:
原文地址:http://www.cnblogs.com/gc812/p/5792592.html