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. Redis 该选择哪种持久化配置

    这个标题或许会让你想起<黑客帝国>里经典的台词,你要选择蓝色药丸,还是红色药丸? Redis 是我们重度使用的一个开源软件,对它的持久化配置做一番相对深入的总结,是值得的.目前它有两种主流 ...

  2. hdu 1429 胜利大逃亡(续)(bfs+状态压缩)

    Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带 ...

  3. pyqt字符串分离开,放入列表中

    string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...

  4. C# 制作 仪表

    以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ...

  5. VC皮肤库SkinSharp 1.0.6.6的使用

    SkinSharp又称Skin#,是Windows环境下一款强大的换肤组件. SkinSharp作为换肤控件,仅仅须要在您的程序中加入一行代码,就能让您的界面焕然一新,并拥有多种主题风格和色调的动态切 ...

  6. CLR via C# - GC

    //像背书一样,记录下吧 1.CLR分配资源 托管堆上维护着一个指针NextObjPtr.该指针表示下一个新建对象在托管堆上的位置.C#的new Object会产生IL newobj指令,NextOb ...

  7. MySql命令——表相关

    auto_increment //自动增长 primary key(id) //指定主键 select last_insert_id();//获得添加列的主键值 create table produc ...

  8. vs2013 JS代码提示

    1.JS提示 在Js文件头部加 /// <reference path="ext-all-dev.js" /> 要求引用的js和本js在同一目录,否则需要全部路径

  9. /调整button的title的位置

    [bottomButton setTitleEdgeInsets:UIEdgeInsetsMake(10, -190, 10, 44)];  //上左下右            ||button.co ...

  10. VisualStudio2013快捷键

    visual studio 2013 是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.VS 2013 中新增了很多提高 ...