zoukankan      html  css  js  c++  java
  • POJ1020(小正方形铺大正方形)

    Anniversary Cake
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 16579   Accepted: 5403

    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 <string.h>
    #include <algorithm>
    using namespace std;
    const int MAXN=165;
    int side,n;
    int square[MAXN],vis[MAXN];
    int mz[MAXN];
    bool comp(int a,int b)
    {
        return a > b;
    }
    bool dfs(int dep)
    {
        if(dep==n)
        {
            return true;
        }
        int mn=MAXN;
        int k;
        for(int i=1;i<=side;i++)
        {
            if(mn>mz[i])//从最左边开始填 
            {
                mn=mz[i];
                k=i;
            }
        }
        for(int i=0;i<n;i++)
        {
            if(!vis[i]&&mn+square[i]<=side&&k+square[i]-1<=side)//检查行,列是否越界 
            {
                int wide=0;
                for(int j=k;j<=side;j++)
                {
                    if(mz[j]==mn)
                    {
                        wide++;
                    }
                    else break;
                }
                if(wide>=square[i])//buf可以容下第i个正方形 
                {
                    vis[i]=1;
                    for(int j=k;j<=k+square[i]-1;j++)
                    {
                        mz[j]+=square[i];
                    }
                    if(dfs(dep+1))
                    {
                        return true;
                    }
                    for(int j=k;j<=k+square[i]-1;j++)
                    {
                        mz[j]-=square[i];
                    }
                    vis[i]=0;
                }
                while(square[i]==square[i+1])    i++;//重要剪枝 
                if(k==1)    return false;
            }
        }
        return false;
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            memset(mz,0,sizeof(mz));
            memset(vis,0,sizeof(vis));
            int sum=0;
            cin>>side;
            cin>>n;
            for(int i=0;i<n;i++)
            {
                cin>>square[i];
                sum+=square[i]*square[i];
            }
            if(side*side!=sum)
            {
                cout<<"HUTUTU!"<<endl;
                continue;
            }
            sort(square,square+n,comp);//小正方形灵活性强,先填大的 
            if(dfs(0))
            {
                cout<<"KHOOOOB!"<<endl;
            }
            else
            {
                cout<<"HUTUTU!"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    css(一)
    Html table
    Html
    jQuery事件和效果
    jQuery基础
    ajax和http状态码
    点击数组选中某一项,存入数组,再次点击删除
    单行两行,多余显示省略号
    git的使用
    产生滚动效果
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5692220.html
Copyright © 2011-2022 走看看