Violent enumeration, DFS (triangular fence, hdu 4277) _ Violent enumeration

Source: Internet
Author: User
Tags time limit

Reason, really some inexplicable, the Dfs belt should be less than the parameters should be faster, because without copying so many parameters, directly modify the global variable or pass the pointer. But in fact, with more parameters than fast 300ms, really fast a lot of time limit 1500ms, with parameter 1000ms, without the parameter 1300ms. It's so weird ... Is the address too far, the addressing is too slow ...

And also.. If your code is about 1500ms, it's still a question of probability, good luck and bad luck. It's really drunk.

But reducing the call to a function can be significantly faster.

So write the code in the future or try to write a little bit of detail, maybe it is not the algorithm you have a problem, but some of the details are not well handled.

At the beginning of the timeout, modified some details on the past.

1, to determine whether OK no longer write a separate function, directly with DFS completed.

2, map changed to set.

3, in the beginning will also be a,b,c ascending sort, and then judge, in fact, is not not in ascending order directly discarded.

4, weighing only need a make_pair (a,b) is enough, do not need to take C.

5, at the beginning also wrote a structure, it is a waste of time.

6, the long long to int, this estimate is useless.


N<=15, decisively on the violence enumerated O (3^n).


Code

#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;

int fen[20];
int ans;
int n;
typedef pair<int,int>pii;
set<pii>s;

void Dfs (int cur,int a,int b,int c)
{
    if (cur>n)
    {
        if (a==0| | b==0| | c==0) return;
        if (a>c| | b>c| | A>B) return;
        PII P=make_pair (a,b);
        if (S.count (p)) return;
        S.insert (p);
        if (a+b<=c) return;
        ans++;
        return;
    }
    DFS (CUR+1,A+FEN[CUR],B,C);
    DFS (CUR+1,A,B+FEN[CUR],C);
    DFS (Cur+1,a,b,c+fen[cur]);

int main ()
{
    int t;
    scanf ("%d", &t);
    while (t--)
    {
        ans=0;
        S.clear ();
        scanf ("%d", &n);
        for (int i=1;i<=n;i++) scanf ("%d", &fen[i]);
        DFS (1,0,0,0);
        printf ("%d\n", ans);
    }
    return 0;
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.