poj 1020 Anniversary Cake简单dfs

    技术2022-05-20  43

    Anniversary Cake Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11377 Accepted: 3574

    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! #include<iostream>#include<cstdio>#include<cstring>using namespace std;int pos[50],cake[15];//pos[i]第i列已经用了pos[i]行,cake[i]边长为i的有cake[i]个int n,m;//盘子边长,个数bool dfs(int id){    if(id==m) return true;    //找出用的最少的那一列    int minc=100,c=-1;    for(int i=1;i<=n;i++)    {        if(pos[i]<minc) minc=pos[i],c=i;    }    //枚举小矩形蛋糕的边长    for(int i=10;i>=1;i--)    {        if(cake[i]&&minc+i<=n&&c+i-1<=n)        {            int flag=1;            for(int j=c;j<=c+i-1;j++)            {                if(pos[j]>minc)                {                    flag=0;break;                }            }            if(!flag) continue;            cake[i]--;            for(int j=c;j<=c+i-1;j++) pos[j]+=i;            if(dfs(id+1)) return true;            cake[i]++;            for(int j=c;j<=c+i-1;j++) pos[j]-=i;        }    }    return false;}int main(){    int ci;scanf("%d",&ci);    while(ci--)    {        memset(cake,0,sizeof(cake));        memset(pos,0,sizeof(pos));        scanf("%d%d",&n,&m);        int sum=0;        for(int i=0;i<m;i++)        {            int x;scanf("%d",&x);            sum+=x*x;cake[x]++;        }        if(sum!=n*n) {printf("HUTUTU!/n");continue;}        if(dfs(0)) printf("KHOOOOB!/n");        else printf("HUTUTU!/n");    }    return 0;}

    最新回复(0)