符号三角形(hdu 2510 搜索+打表)

符号三角形

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1246    Accepted Submission(s): 664


Problem Description
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
 

 

Input
每行1个正整数n <=24,n=0退出.
 

 

Output
n和符号三角形的个数.
 

 

Sample Input
15
16
19
20
0
 
Sample Output
15 1896
16 5160
19 32757
20 59984
 
打表代码:
 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 }

 

posted @ 2015-12-06 19:01  御心飞行  阅读(315)  评论(0编辑  收藏  举报