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. .NET中的PublicKeyToken以及强命名问题

    在.NET的GAC出现之前,以前有DLL Hell的问题.这是由于当时对于共享的DLL的处理方式.是通过採用注冊表的方式实现的.当我们安装一个程序A的时候,这个程序包括一个共享的DLL,那么这个DLL ...

  2. SPOJ 15. The Shortest Path 最短路径题解

    本题就是给出一组cities.然后以下会询问,两个cities之间的最短路径. 属于反复询问的问题,临时我仅仅想到使用Dijsktra+heap实现了. 由于本题反复查询次数也不多,故此假设保存全部最 ...

  3. 用block变量来对字符数组对象进行排序

    <span style="font-size:18px;">降序排序</span> <span style="font-size:18px; ...

  4. mysql对表列数和行大小的限制

    mysql对表列数和行大小的限制 - CSDN博客 https://blog.csdn.net/Dr_Joseph/article/details/78111312

  5. JavaScript全局属性/函数

    JavaScript 全局属性和方法可用于创建Javascript对象. JavaScript 全局属性 属性 描述 Infinity 代表正的无穷大的数值. NaN 指示某个值是不是数字值. und ...

  6. 深度理解apache 重写模块rewrite_mod,重写不再犯错

    1.RewriteRule ^(com\/.*)$ index.php?do=$1 问:上面的规则匹配表达式 "^(.*)$" 匹配的内容是什么 答:匹配内容是URI站点目录:/d ...

  7. hihocoder1699

    链接:http://hihocoder.com/problemset/problem/1699 快毕业了的菜菜,做了个比赛,遇到四维偏序,调成了傻逼,所以记录下,看了下以前的傻逼代码,发现自己的cdq ...

  8. 两道NOIP里的DP题目~

    拦截导弹    来源:NOIP1999(提高组) 第一题 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都 ...

  9. Java日期推迟计算、日期大小

    /** * 日期推迟计算 * @param date * @param num * @return java.util.HashMap<java.lang.String,java.lang.St ...

  10. luogu 3953 逛公园

    noip2017 D1T3 逛公园 某zz选手看到数据范围直接就最短路计数了,结果写错了爆零 题目大意: N个点M条边构成的有向图,且没有自环和重边.其中1号点是起点,N号点是公园的终点,每条边有一个 ...