Anniversary Cake

Time Limit: 1000MS
Memory Limit: 10000KB
64bit IO Format: %I64d & %I64u

Submit Status

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!
题目简单翻译:
给你一个方形蛋糕,问能不能把它切成m个小蛋糕,而不浪费。
 
解题思路:
主要思路来源于優YoU,大致是从下往上填格子。
 
代码:
#include<iostream>
#include<cstring>
using namespace std; int BoxSize;
int n;
int SizeNum[];
int col[]; bool DFS(int FillNum)
{
if(FillNum==n) return true;
int min=;
int prow;
for(int i=;i<=BoxSize;i++)
if(min>col[i])
{
min=col[i];
prow=i;
}
for(int size=;size>=;size--)
{
if(!SizeNum[size]) continue;
if(BoxSize-col[prow]>=size&&BoxSize-prow+>=size)
{
int wide=;
for(int r=prow;r<=prow+size-;r++)
{
if(col[r]<=col[prow])
{
wide++;
continue;
}
break;
} if(wide>=size)
{
int r;
SizeNum[size]--;
for(r=prow;r<=prow+size-;r++)
col[r]+=size;
if(DFS(FillNum+)) return true;
SizeNum[size]++;
for(r=prow;r<=prow+size-;r++)
col[r]-=size;
}
} }
return false;
} int main(void)
{
int test;
cin >> test;
for(int t=;t<=test;t++)
{
memset(SizeNum,,sizeof SizeNum);
memset(col,,sizeof col);
cin >> BoxSize >> n;
int cnt=,area=;
for(int i=;i<=n;i++)
{
int size;
cin >> size;
area+=size*size;
SizeNum[size]++;
if(size>BoxSize/) cnt++;
}
if(cnt>||area!=BoxSize*BoxSize)
{
cout << "HUTUTU!" << endl;
continue;
}
if(DFS()) cout << "KHOOOOB!" << endl;
else cout << "HUTUTU!" << endl;
}
}

POJ 1020 Anniversary Cake(DFS)的更多相关文章

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

                                                                                                         ...

  2. POJ 3009-Curling 2.0(DFS)

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12158   Accepted: 5125 Desc ...

  3. 题解报告:poj 1321 棋盘问题(dfs)

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  4. POJ 2251 Dungeon Master(dfs)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  5. [ACM] POJ 3740 Easy Finding (DFS)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16202   Accepted: 4349 Description Give ...

  6. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  7. POJ 1321 棋盘问题(dfs)

    传送门 棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38297   Accepted: 18761 Descri ...

  8. POJ 1321 棋盘问题 (dfs)

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  9. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

随机推荐

  1. CoFun 1613 单词连接

    Description Stan有N个不同的单词,这天,Stan新结交的两个朋友来他这里玩,Stan作为主人,他需要送给他们单词,但由于Stan不能偏心,所以Stan给每个单词一个权值v_i,他需要他 ...

  2. LeetCode_Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given -> ...

  3. 董事长、总裁与CEO的区别与实质

    自从信息产业兴起以来,尤其是网络股泡沫产生以来,“CEO”在中国骤然成为一个流行词汇.总经理和总裁们纷纷改称CEO,这个缩写词比它的中译版“首席执行官”更简洁,在中国人心目中更有神圣感,于是便出现了今 ...

  4. XML Schema <第三篇>

    验证XML文档是否符合议定的XML结构有两种方法,分别是DTD模式与XML Schema.本文主要介绍XML Schema. 一.XML Schema的优点 XML Schema基于XML,没有专门的 ...

  5. bzoj1379 [Baltic2001]Postman

    Description 邮递员每天给N个村子的人送信,每个村子可能在某个十字路口上,或一条路的中央. 村子里的人都希望早点收到信,因此与邮递员达成一个协议:每个村子都有一个期望值Wi,如果这个村子是邮 ...

  6. STL中istream_iterator和ostream_iterator的基本用法

    标准程序库定义有供输入及输出用的iostream iterator类,称为istream_iterator和ostream_iterator,分别支持单一型别的元素读取和写入.使用这两个iterato ...

  7. mysql5.5 无法创建实例,error 16001

    今天想用jdbc做个小程序,结果发现好久不用的mysql不好用了,我装的是社区版(win7)环境下,按理说不可能出问题,找了一堆解决方案都没解决,准备重装的时候想把mysql服务停了,直接在dos输入 ...

  8. 【CF689D Friends and Subsequences】二分搜索,区间查询

    题意:给定两个整数序列a,b,将a,b对齐,问有多少个区间满足a的区间内最大值等于b的区间内最小值. 数据范围:区间长度n属于[1, 200000],序列中的元素在整型范围内 思路:枚举所有n*(n+ ...

  9. 你了解JS执行过程吗?

        正如我们了解的一样,当我们书写了JS程序之后,打开浏览器,我们的代码就可以开始运行了(当然保证你的代码没有问题,才能按照你的预期进行执行).刚才说的是JS执行的一个大的环境,今天我们学习一下, ...

  10. Android.mk具体解释

    概述     Android.mk文件用来向编译系统描写叙述怎样编译你的源码.更确切地说,该文件事实上就是一个小型的Makefile.由于该文件会被NDK的编译工具解析多次,因此应该尽量降低源码中声明 ...