首页 > ACM题库 > HDU-杭电 > hdu 2510 符号三角形-计算几何-[解题报告]C++
2014
02-09

hdu 2510 符号三角形-计算几何-[解题报告]C++

符号三角形

问题描述 :

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

输入:

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

输出:

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

样例输入:

15
16
19
20
0

样例输出:

15 1896
16 5160
19 32757
20 59984

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2510

数据范围很小.一看就知道可以用模拟搜索解,然后打表.

 

搜索程序

#include <iostream>
using namespace std;

int map[25][25];
int n;
int DFS(int d,int num)
{
    if(num > n*(n+1)/4) return 0;//小小的剪枝,+号大于总数的1/2时,不满足条件
    if(d>=n)
    {
        if(n*(n+1)/2 == 2 * num)
        {
            return 1;
        }else return 0;
    }
    if(d==0)
    {
        map[0][0] = 1;
        int sum = DFS(1,1) + DFS(1,1);
        map[0][0] = 0;
        sum += DFS(1,0) + DFS(1,0);
        return sum;
    }
    else 
    {
      int sum = 0;
      for(int k=0;k<2;k++)
      {
          int t = num;
          for(int j=0;j<=d;j++)
          {
            if(j==0)
            {
                map[d][0] = k;
                t+= k;
            }
            else 
            {
                map[d][j] = map[d-1][j-1] ^ map[d][j-1];
                t+=map[d][j];
            }
          }
        sum += DFS(d+1,t);
      }
        return sum;
    }
}
int main(int argc, const char *argv[])
{
  freopen("output.txt","r",stdout);
    for(int i=1;i<=24;i++)
    {
        n = i;
        if(n*(n+1)/2%2!=0) cout<<"0,";
        else
        {
            int ans = DFS(0,0);
            cout<<ans/2<<",";
        }
    }
    return 0;
}

 

打表程序

#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
    int n;
    int ans[24] = {0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229};
    while(cin>>n&&n)
    {
        cout<<n<<" "<<ans[n-1]<<endl;
    }
    return 0;
}

 

解题转自:http://www.cnblogs.com/destino74/archive/2013/09/11/3314909.html


  1. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.