zoukankan      html  css  js  c++  java
  • POJ1020 Anniversary Cake

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 16683   Accepted: 5442

    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

    大致意思是,有一块儿边长为s的正方形大蛋糕,问是否能不浪费地分割成n个边长不同的小蛋糕。可行则输出KHOOOOB!否则输出HUTUTU!

    逆向思维,尝试用n个边长不同的小蛋糕拼成一个边长为s的正方形大蛋糕

    搜索解决。为了便于处理状态,严格从左往右,从下往上地加入小蛋糕,填满下面的行才能填上面的行。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 using namespace std;
     6 int T,n,m;
     7 int a[11];//边长为[i]的蛋糕的数量 
     8 int ck[50];//第[i]列已填的高度 
     9 bool dfs(int y){
    10     if(y==m)return 1;
    11     int i,j;
    12     //
    13     int mi=200,pos=0;
    14     for(j=1;j<=n;j++)
    15         if(ck[j]<mi){
    16             mi=ck[j];
    17             pos=j;    
    18         }
    19     //
    20     for(i=10;i;i--){
    21         if(!a[i])continue;//没有该尺寸蛋糕,跳过 
    22         if(pos+i-1>n)continue;//该尺寸宽度比剩余宽度大,跳过 
    23         for(j=0;j<i;j++){
    24             if(ck[pos+j]<=ck[pos] && ck[pos+j]+i<=n)continue;
    25             else break;
    26         }
    27         if(j<i)continue;//高度不足,跳过 
    28         for(j=0;j<i;j++)ck[pos+j]+=i;
    29         a[i]--;
    30         if(dfs(y+1))return 1;
    31         a[i]++;
    32         for(j=0;j<i;j++)ck[pos+j]-=i;
    33     }
    34     return 0;
    35 }
    36 int main(){
    37     scanf("%d",&T);
    38     int i,j;
    39     while(T--){
    40         memset(ck,0,sizeof ck);
    41         memset(a,0,sizeof a);
    42         scanf("%d%d",&n,&m);
    43         int x;
    44         int ssum=0;
    45         int cnt=0;
    46         for(i=1;i<=m;i++){
    47             scanf("%d",&x);
    48             a[x]++;
    49             ssum+=x*x;
    50             if(x>n/2)cnt++;//尺寸大于n/2的蛋糕若有多块,肯定不可行 
    51         }
    52         if(ssum!=n*n || cnt>1){
    53             printf("HUTUTU!
    ");
    54             continue;
    55         }
    56         if(!dfs(0)){
    57             printf("HUTUTU!
    ");
    58         }
    59         else printf("KHOOOOB!
    ");
    60         
    61     }
    62     return 0;
    63 }    
  • 相关阅读:
    Linux下修改Mysql的用户(root)的密码
    Mysql 用户权限管理
    mysql的四种启动方式
    MySQL慢日志的相关参数
    mysqlsla slow-query常用用法

    导包和模块注意的问题
    Python面向对象之魔法方法/双下方法
    __new__问题
    转:Python常见字符编码及其之间的转换
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5793839.html
Copyright © 2011-2022 走看看