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. 分布式批处理平台(wolf)简介

    "wolf"即狼,狼的适应性很强.可栖息范围包括苔原.草原.森林.荒漠.农田等多种生境:具有敏锐的观察力.专一的目标.默契的配合,它们总能依靠团体的力量达成目标. 分布式批处理平台 ...

  2. java读取property文件

    property文件中: url = jdbc:mysql://localhost:3306/resume   user= root   pwd = 123 java代码读取:       packa ...

  3. LeetCode_Distinct Subsequences

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. BIN和HEX文件的区别

    HEX文件和BIN文件是我们经常碰到的2种文件格式.下面简单介绍一下这2种文件格式的区别: 1.HEX文件是包括地址信息的,而BIN文件格式只包括了数据本身.在烧写或下载HEX文件的时候,一般都不需要 ...

  5. Box.net获5000万美元投资(SAAS博士)

    9月29日,据国外媒体报道,“云”存储服务商Box.net已经从Salesforce等投资者获得新一轮融资5000万美元. Box.net过去的投资者也参与了新一轮的融资.最终,在CRM巨人Sales ...

  6. JavaEE连接池泄漏问题检测Oracle数据库

    1.项目环境 项目是典型的轻量级JavaEE项目,使用SSH框架构建,数据源使用DBCP管理,和Spring进行了整合. 项目数据库使用Oracle数据库. 项目DBCP配置内容如下 ###### D ...

  7. bzoj1751 [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 168  Solved: 130 [ ...

  8. WIN8共享文件 详细设置

    原文地址:http://jingyan.baidu.com/article/75ab0bcbff4274d6864db2f5.html win8共享文件设置 详细教程 | 浏览:6987 | 更新:2 ...

  9. 使用Windows USB-DVD制作U盘启动安装系统盘

    第一步:到如下所示的地址下载所需要的*.iso系统镜像文件. http://msdn.itellyou.cn/ 第二步:下载Windows USB-DVD工具 https://www.microsof ...

  10. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...