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0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 No. 9 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 ...以下省略
1 #include<cstdio> 2 #define s printf( 3 #define h m[i][j]=x[j]=y[i+j]=z[i-j+10]= 4 #define f(i) for(int i=0;i<8;i++) 5 6 int m[9][9],c,k,l,x[9],y[9],z[50]; 7 8 int d(int i) 9 { 10 if(i>7) 11 { 12 ++c; 13 s"No. %d\n",c); 14 f(k) 15 { 16 f(l)s"%d ",m[l][k]); 17 s"\n"); 18 } 19 } 20 f(j) 21 { 22 if(!x[j]&&!y[i+j]&&!z[i-j+10]) 23 { 24 h 1; 25 d(i+1); 26 h 0; 27 } 28 } 29 } 30 31 int main() 32 { 33 d(0); 34 }
2 1 92
15863724 84136275
1 #include<iostream> 2 #include<cstdio> 3 #include<iostream> 4 #include<cstdlib> 5 #include<cstring> 6 #include<string> 7 #include<cmath> 8 9 using namespace std; 10 11 bool d[100]= {0},b[100]= {0},c[100]= {0}; //b为行;c,d为对角线,c和,d差 12 int h=0,n,cs[99],a[100]; 13 14 int print() { 15 h++; 16 for(int j=1; j<=n; j++) { 17 if(h==cs[j]) { 18 j++; 19 for(int i=1; i<=8; i++) { 20 cout<<a[i]; 21 } 22 cout<<endl; 23 } 24 } 25 } 26 27 int search(int i) { 28 int j; 29 for(j=1; j<=8; j++) { 30 if(!b[j]&&!c[i+j]&&!d[i-j+7]) { 31 a[i]=j; 32 b[j]=1; 33 c[i+j]=1; 34 d[i-j+7]=1; 35 if(i==8) print(); 36 else search(i+1); 37 b[j]=0; 38 c[i+j]=0; 39 d[i-j+7]=0; 40 } 41 } 42 } 43 44 int main() { 45 cin>>n; 46 for(int i=1; i<=n; i++) { 47 cin>>cs[i]; 48 } 49 search(1);//从第1个皇后开始放置 50 return 0; 51 }
标签:n皇后 ring 方法 str namespace blank cst 八皇后 com
原文地址:http://www.cnblogs.com/zxqxwnngztxx/p/6758900.html