Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15704   Accepted: 5123

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
题解:可理解为将多个正方形小蛋糕放入一个正方形蛋糕盒子。从左往右,从前往后放入。简单的DFS
 
代码:
 #include<iostream>
#include<cstring>
using namespace std;
int col[]; //将蛋糕分为1*1的小块,下标表示列,值表示用到第几行
int cakesize; //蛋糕大小
int part[]; //数组值为该大小的小块的个数
int num; //蛋糕个数 bool DFS(int fillnum) //从前往后,从左往右放入
{
int min=;
int flag;
int wide;
if(fillnum==num)
return true;
for(int i=; i<=cakesize; i++) //记录所有列里所用最少的
if(min>col[i])
{
min=col[i];
flag=i;
}
for(int size=; size>; size--) //从大到小遍历,从大的开始放,越小灵活性越大
{
if(!part[size])
continue;
if(cakesize-min>=size&&cakesize-flag+>=size) //判断蛋糕放入‘是否有可能’溢出,是否有‘可能’放入
{
wide=; //之前错在这里
for(int j=flag; j<=flag+size-; j++) //与上面的if判断一起,其作用为判断是否能放下该块蛋糕
{
if(col[j]<=min)
wide++;
else
break;
}
if(wide>=size)
{
part[size]--;
for(int k=flag; k<=flag+size-; k++)
col[k]+=size;
if(DFS(fillnum+))
return true;
part[size]++; //回溯
for(int k=flag; k<=flag+size-; k++)
col[k]-=size;
}
}
}
return false;
} int main()
{
int t,side;
cin>>t;
while(t--)
{
memset(part,,sizeof(part));
memset(col,,sizeof(col));
cin>>cakesize;
cin>>num;
for(int i=; i<=num; i++)
{
cin>>side;
part[side]++;
}
if(DFS())
cout<<"KHOOOOB!"<<endl;
else
cout<<"HUTUTU!"<<endl;
}
return ;
}

Anniversary Cake的更多相关文章

  1. POJ 1020 Anniversary Cake(DFS)

    Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...

  2. poj 1020 Anniversary Cake(切正方形蛋糕+搜索)

                                                                                                         ...

  3. 【DFS】Anniversary Cake

    [poj1020]Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17203   Accep ...

  4. POJ1020 Anniversary Cake

    题目来源:http://poj.org/problem?id=1020 题目大意:有一块边长为s的正方形大蛋糕,有n个客人,每个客人想分一块边长为si的正方形蛋糕.求这块大蛋糕能否恰好满足所有客人的需 ...

  5. 【poj1020】 Anniversary Cake

    http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像 ...

  6. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...

  7. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  8. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  9. POJ1020(小正方形铺大正方形)

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

随机推荐

  1. AE After Effect 如何替换和修改素材

    替换素材:如图所示,相框外的人是成双成对的,相框里面的却只有一个人,我们想要把这个素材替换成两个人的.我们鼠标放到视频预览框的任何一个元素上面底部都会提示这是什么素材.比如我放到一个人的上面,则该人物 ...

  2. java 代理模式具体解释

    java 动态代理(JDK和cglib) 设计模式这东东每次看到就明确可过段时间又不能非常流利的说出来.今天就用具体的比喻和实例来加深自己的理解(小弟水平不高有不正确的地方希望大家能指出来). (1) ...

  3. C++学习之构造函数中的异常处理

    构造函数中可不可以抛出异常?当然可以.从语法上来说,是可以的:从实际情况来看,现在的软件系统日渐庞大和复杂,很难保证 Constructor 在执行过程中完全不发生一点异常. 那么,如果构造函数中抛出 ...

  4. MariaDB ----单表查询

    1>按一定条件查询某字段的数据 (where) ; ( 查询 id > 的数据) #补充: ; 注意“ select * from  students1:  (此命令需谨慎使用, 数据量大 ...

  5. B. Amr and The Large Array(Codeforces Round #312 (Div. 2)+找出现次数最多且区间最小)

    B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...

  6. 有意思的RTL函数RegisterClass(在持久化中,你生成的一个新类的对象,系统并不知道他是如何来的,因此需要你注册)good

    例子1:Delphi中使用纯正的面向对象方法(这个例子最直接) Delphi的VCL技术使很多程序员能够非常快速的入门:程序员门只要简单的拖动再加上少量的几个Pascal语句,呵呵,一个可以运行得非常 ...

  7. 【Codevs 2630】宝库通道

    http://codevs.cn/problem/2630/ Solution 预处理f[i][j],代表第j列前i行的代价 枚举上下界,然后做最大子段和,g[i]代表选到第i列的代价, g[k]=( ...

  8. python调用java程序--jpype

    官方网站:http://jpype.sourceforge.net/ 官方使用文档:http://jpype.sourceforge.net/doc/user-guide/userguide.html ...

  9. 两个局域网(办公网-IDC)安全互通方案2:by GRE and linux server&深入理解GRE

    (0)gre的turnel的打通 1. 这个过程就是双方建立turnel的过程.           (1)局域网路由过程 1.主机A发送一个源为192.168.1.2,目的为10.1.1.2的包 ( ...

  10. (博弈论)51NOD 1066 Bash游戏

    有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N和K,问最后谁能赢得比赛. 例如N = 3 ...