zoukankan      html  css  js  c++  java
  • POJ 1020 Anniversary Cake(DFS)

    Anniversary Cake

    Time Limit: 1000MS
    Memory Limit: 10000KB
    64bit IO Format: %I64d & %I64u

    Submit Status

    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!
    题目简单翻译:
    给你一个方形蛋糕,问能不能把它切成m个小蛋糕,而不浪费。
     
    解题思路:
    主要思路来源于優YoU,大致是从下往上填格子。
     
    代码:
    #include<iostream>
    #include<cstring>
    using namespace std;
    
    int BoxSize;
    int n;
    int SizeNum[11];
    int col[41];
    
    bool DFS(int FillNum)
    {
        if(FillNum==n) return true;
        int min=50;
        int prow;
        for(int i=1;i<=BoxSize;i++)
            if(min>col[i])
            {
                min=col[i];
                prow=i;
            }
        for(int size=10;size>=1;size--)
        {
            if(!SizeNum[size]) continue;
            if(BoxSize-col[prow]>=size&&BoxSize-prow+1>=size)
            {
                int wide=0;
                for(int r=prow;r<=prow+size-1;r++)
                {
                    if(col[r]<=col[prow])
                    {
                        wide++;
                        continue;
                    }
                    break;
                }
    
                if(wide>=size)
                {
                    int r;
                    SizeNum[size]--;
                    for(r=prow;r<=prow+size-1;r++)
                        col[r]+=size;
                    if(DFS(FillNum+1)) return true;
                    SizeNum[size]++;
                    for(r=prow;r<=prow+size-1;r++)
                        col[r]-=size;
                }
            }
    
        }
        return false;
    }
    
    int main(void)
    {
        int test;
        cin >> test;
        for(int t=1;t<=test;t++)
        {
            memset(SizeNum,0,sizeof SizeNum);
            memset(col,0,sizeof col);
            cin >> BoxSize >> n;
            int cnt=0,area=0;
            for(int i=1;i<=n;i++)
            {
                int size;
                cin >> size;
                area+=size*size;
                SizeNum[size]++;
                if(size>BoxSize/2) cnt++;
            }
            if(cnt>1||area!=BoxSize*BoxSize)
            {
                cout << "HUTUTU!" << endl;
                continue;
            }
            if(DFS(0)) cout << "KHOOOOB!" << endl;
            else cout << "HUTUTU!" << endl;
        }
    }
  • 相关阅读:
    简爱 灵魂所在
    charles抓取http/https
    Class.forName()用法
    ArrayList源码剖析
    java中的多线程
    分布式负载均衡缓冲系统,如何快速定位到是那个服务器
    maven依赖jar包时版本冲突的解决
    简单工厂模式设计(java反射机制改进)
    Fiddler 抓包工具使用详解
    Fiddler 使用
  • 原文地址:https://www.cnblogs.com/I-love-HLD/p/4625371.html
Copyright © 2011-2022 走看看