Anniversary Cake           
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! 题意:将一个大的正方形的蛋糕恰好切成多个小正方形,问能否做到。
解析:首先判断所有小正方形的面积之和是否等于大正方形,不等肯定不行。还有如果小正方形中存在两个最大的边长之和大于大正方形,肯定也不行。搜索的话,由于小正方形的边长最大为10,而且最多只有16个,因此大正方形的边长最大为40
那么我从底层开始一层层往上铺,用一个col[]数组保存这一列对应的高度,要铺的话总是找col[]中最小最左边的(铺最低的地方)还要判断能不能铺某个正方形,直到所有正方形铺完。 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int edge,N;
int H[11],col[41];
bool dfs(int cur)
{
if(cur==N) return true; //铺完了
int minX=50,pos;
for(int i=1;i<=edge;i++) if(col[i]<minX) { minX=col[i],pos=i; } //找到最低最靠左的位置
for(int i=10;i>=1;i--) //枚举正方形高度
{
if(!H[i]) continue;
if(minX+i<=edge&&pos+i<=edge+1) //不能越界
{
bool ok=true;
for(int st=pos;st<pos+i;st++) //还要判断能不能铺
{
if(col[st]>col[pos]){ ok=false; break; }
}
if(ok)
{
for(int st=pos;st<pos+i;st++) col[st]+=i; //铺的位置全部加上这个高度
H[i]--; //删掉这个正方形
if(dfs(cur+1)) return true;
H[i]++;
for(int st=pos;st<pos+i;st++) col[st]-=i;
}
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>edge>>N;
memset(H,0,sizeof(H));
memset(col,0,sizeof(col));
int sum=0,cnt=0;
for(int i=1;i<=N;i++)
{
int size;
cin>>size;
H[size]++;
sum+=size*size;
if(size>edge/2) cnt++;
}
if(cnt>1||edge*edge!=sum) //事先预判断
{
cout<<"HUTUTU!"<<endl;
continue;
}
if(dfs(0)) cout<<"KHOOOOB!"<<endl;
else cout<<"HUTUTU!"<<endl;
}
return 0;
}
 
 

poj 1020 Anniversary Cake(切正方形蛋糕+搜索)的更多相关文章

  1. POJ 1020 Anniversary Cake(DFS)

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

  2. POJ1020 Anniversary Cake

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

  3. 【DFS】Anniversary Cake

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

  4. Anniversary Cake

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

  5. poj 2324 Anniversary party(树形DP)

    /*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...

  6. POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)

    POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...

  7. DP Intro - poj 2342 Anniversary party

    今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...

  8. 洛谷P1528 切蛋糕 [搜索,二分答案]

    题目传送门 切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把刀 ...

  9. 【poj1020】 Anniversary Cake

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

随机推荐

  1. POJ3669(Meteor Shower)(bfs求最短路)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12642   Accepted: 3414 De ...

  2. jumpGate部署

    preface statement: manageing OpenStack & SoftLayer resource with Jumpgate 1,forward; Imagine a w ...

  3. OpenStack术语名词及问题调试

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAnoAAAEkCAIAAAA3pAtBAAAgAElEQVR4nOy953tUx7o9uCrt2EmtHB

  4. Linux常用命令及使用技巧

    本文重点讲述Linux命令的使用,命令是学习Linux必须熟练掌握的一个部分.Linux下的命令大概有600个,而常用的命令其实只有80个左右,这些常用的命令是需要灵活掌握的.虽然Linux的各个发行 ...

  5. WEB应用知识一二三

    1.HTTP协议 |--基于请求(Request)和响应(Response)的无状态通讯协议 浏览器和WEB应用程序通过HTTP进行通信.客户端通过URL对指定服务器要求特定位置的数据 |--POST ...

  6. linux用户创建删除以及文件权限查看修改

    一. 1.查看用户 命令如下:whoami 2.创建用户 创建用户命令:sudo adduser hello 超级用户是 root 删除用户名命令:sudo deluser hello --remov ...

  7. XtraReport改变纸张方向

    XtraReport纸张方向改变可以通过修改Landscape属性: Landscape=true 为横向输出 Landscape=false 为纵向输出

  8. 0118——RTLabel和正则表达式

    RTLabel和RegexKitLite都要导入第三方库 使用Regexkitlite库进行正则表达式的解析 1.库是使用MRR,如果在ARC工程里面使用这个类,必须在project->buil ...

  9. 编写简单的 NT 式驱动程序的加载与卸载工具

    写驱动的加载需要用到五个函数: OpenSCManager() CreateService() OpenService() StartService() CloseServiceHandle() 这五 ...

  10. hdu3830 (二分+LCA)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Checkers Time Limit: 2000/1000 MS (Java/O ...