标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1246 Accepted Submission(s):
664
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 int a[25]; 6 char res[25][25]; 7 int sum; 8 bool count(int n) 9 { 10 int p1=0,p2=0; 11 int i,j; 12 for(i=1;i<=n;i++) 13 { 14 if(res[0][i]==‘+‘) p1++; 15 else p2++; 16 } 17 for(int i=1;i<n;i++) 18 for(j=1;j<=n-i;j++) 19 { 20 res[i][j]=(res[i-1][j]==res[i-1][j+1]?‘+‘:‘-‘); 21 if(res[i][j]==‘+‘) p1++; 22 else p2++; 23 } 24 if(p1==p2) 25 return 1; 26 return 0; 27 } 28 void dfs(int n,int s) 29 { 30 int i,j; 31 if(s>n) 32 { 33 if(count(n)) 34 sum++; 35 return; 36 } 37 res[0][s]=‘+‘; 38 dfs(n,s+1); 39 res[0][s]=‘-‘; 40 dfs(n,s+1); 41 return; 42 } 43 int main() 44 { 45 int n; 46 int i,j; 47 a[1]=a[2]=0; 48 for(i=1;i<=20;i++) 49 { 50 sum=0; 51 dfs(i,1); 52 a[i]=sum; 53 cout<<a[i]<<endl; 54 } 55 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5024093.html