POJ 1020 Anniversary Cake

Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10380   Accepted: 3238

Description

Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square-shaped chocolate cake with known size. She asks each invited person to determine the size of the piece of cake that he/she wants (which should also be square-shaped). She knows that Mr. Kavoosi would not bear any wasting of the cake. She wants to know whether she can make a square cake with that size that serves everybody exactly with the requested size, and without any waste.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case. Each test case consist of a single line containing an integer s, the side of the cake, followed by an integer n (1 ≤ n ≤ 16), the number of cake pieces, followed by n integers (in the range 1..10) specifying the side of each piece.

Output

There should be one output line per test case containing one of the words KHOOOOB! or HUTUTU! depending on whether the cake can be cut into pieces of specified size without any waste or not.

Sample Input

2
4 8 1 1 1 1 1 3 1 1
5 6 3 3 2 1 1 1

Sample Output

KHOOOOB!
HUTUTU!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
惭愧惭愧,这道题实在搞不清楚搜的策略,还是参考别人的代码写的

 

 

#include

#include

int c[20],n,d[50],s;

bool ok;

void dfs(int depth)

{

    int p,min;

    if(ok) return ;//两个剪枝

    if(depth>=n) { ok=true; return; }

    min=1<<31-1;

    for(int i=1;i<=s;i++)

    if( d[i]这个极其重要,保证了每列覆盖的连续性

    for(int i=10;i>=1;i--)//数据很小,最大是到10

        if(c[i]>0 && min+i-1<=s && p+i-1<=s)//条件

        {

            bool f=true;

            for(int j=p;j<=p+i-1;j++)

            if(d[j]>min)   { f=false;break;}

            if(f)

            {

                for(int j=p;j<=p+i-1;j++) d[j]+=i;

                c[i]--;

                dfs(depth+1);

                c[i]++;

                for(int j=p;j<=p+i-1;j++) d[j]-=i;

            }

        }

}

int main()

{

    int T;

    scanf("%d",&T);

    while(T--)

    {

        memset(c,0,sizeof(c));

        memset(d,0,sizeof(d));

        scanf("%d ",&s);

        scanf("%d ",&n);

        int max=0,sum=0;

        bool flag=true;

        for(int i=1;i<=n;i++)

        {

           int x;

           scanf("%d",&x);

           if(x+max>s) flag=false;

           if(x>max)  max=x;

           sum+=x*x;

           c[x]++;

        }

        if(sum!=s*s)  flag=false;

        ok=false;

        if(flag)  dfs(0);

        if(ok)   puts("KHOOOOB!");

        else puts("HUTUTU!");

    }

    return 0;

}

你可能感兴趣的:(ACM_搜索)