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. redux 存值 及 取值 的操作

    项目目录 首先,一个基于React + Redux + React-Router的项目目录可以按照我下方的图片来构建: 其中assets目录用于存放项目的静态资源,如css/图片等,src目录则用于存 ...

  2. POJ2155 Matrix 【二维树状数组】+【段更新点查询】

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Descripti ...

  3. salt-stack "No Top file or external nodes data matches found"解决

    salt-stack在配置分组时提示如下信息: No Top file or external nodes data matches found 后来在官网上找到如下提示,意思是需要重启master服 ...

  4. js 获取函数的所有参数名

    具体思路: 利用Function.toString()方法,获取到函数的源码,再利用正则匹配获取到参数名字. 实现代码(代码基于ES6): // 获取函数的参数名 function getParame ...

  5. Chrome查看JavaScript函数

    在页面上右键view page source(Ctrl+U),然后在弹出来的界面可以查找JavaScript函数 注意:这个只能看到内嵌在网页上的JavaScript函数 一般来讲,JavaScrip ...

  6. iOS如何查看静态库.a文件支持的cpu类型

    打开终端: 输入 lipo -info 然后将你要查看的静态库.a 文件找到,拖入 -info 后边.假设路径为A,即为 lipo -info A 回车键,然后就会看到静态库是否支持 armv7,ar ...

  7. [疑问] C# 多线程程序,如果在并行程序块中开空间会远远慢于将空间开在并行块之外

    // int[,] label = new int[m, n]; Parallel.For(, thread_num, (n) => { ]; i++) { int[] tmp = new in ...

  8. 利用 C# dynamic 减少创建模型类

    C# 的 dynamic 关键字可以是C#可以像 javascript 这种弱类型语言一样具有随时可以添加属性的能力.C# 是一种强类型语言,dynamic 要摆脱类型的限制,自然是有代价的.这里不讨 ...

  9. python-----tuple用法

    有一种有序列表叫元组:tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样是列出同学的名字: >>> classmates = ('Michael' ...

  10. ODB——基于c++的ORM映射框架尝试(使用)

    摘要: 2.使用 首先,需要定义一个对象,用来和数据库字段对应: [cce lang=”cpp”] #ifndef VOLUME_H #define VOLUME_H #include #includ ...