Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16579   Accepted: 5403

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! 题意:用小正方形去铺大正方形,问是否能恰好铺满。
思路:见代码。
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=;
int side,n;
int square[MAXN],vis[MAXN];
int mz[MAXN];
bool comp(int a,int b)
{
return a > b;
}
bool dfs(int dep)
{
if(dep==n)
{
return true;
}
int mn=MAXN;
int k;
for(int i=;i<=side;i++)
{
if(mn>mz[i])//从最左边开始填
{
mn=mz[i];
k=i;
}
}
for(int i=;i<n;i++)
{
if(!vis[i]&&mn+square[i]<=side&&k+square[i]-<=side)//检查行,列是否越界
{
int wide=;
for(int j=k;j<=side;j++)
{
if(mz[j]==mn)
{
wide++;
}
else break;
}
if(wide>=square[i])//buf可以容下第i个正方形
{
vis[i]=;
for(int j=k;j<=k+square[i]-;j++)
{
mz[j]+=square[i];
}
if(dfs(dep+))
{
return true;
}
for(int j=k;j<=k+square[i]-;j++)
{
mz[j]-=square[i];
}
vis[i]=;
}
while(square[i]==square[i+]) i++;//重要剪枝
if(k==) return false;
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
memset(mz,,sizeof(mz));
memset(vis,,sizeof(vis));
int sum=;
cin>>side;
cin>>n;
for(int i=;i<n;i++)
{
cin>>square[i];
sum+=square[i]*square[i];
}
if(side*side!=sum)
{
cout<<"HUTUTU!"<<endl;
continue;
}
sort(square,square+n,comp);//小正方形灵活性强,先填大的
if(dfs())
{
cout<<"KHOOOOB!"<<endl;
}
else
{
cout<<"HUTUTU!"<<endl;
}
}
return ;
}

POJ1020(小正方形铺大正方形)的更多相关文章

  1. lintcode-436-最大正方形

    436-最大正方形 在一个二维01矩阵中找到全为1的最大正方形 样例 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 返回 4 标签 动态规划 爱彼迎 脸书 思路 使用 ...

  2. 九度OJ1020-最小正方形-判大小

    题目1020:最小长方形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7410 解决:3521 题目描述:     给定一系列2维平面点的坐标(x, y),其中x和y均为整数,要求用一个 ...

  3. 从零开始学ios开发(八):Autorotation and Autosizing

    不好意思,这一篇间隔的时间有点长,最近实在是事情太多,耽搁了,好了,长话短说,下面继续学习ios. 这次学习的内容是Autorotation和Autosizing,Autorotation就是屏幕内容 ...

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

                                                                                                         ...

  5. lintcode:最大子正方形

    题目: Maximal Square Given a 2D binary matrix filled with 0's and 1's, find the largest square contain ...

  6. 2015年第六届蓝桥杯C/C++程序设计本科B组决赛 完美正方形

    完美正方形 如果一些边长互不相同的正方形,可以恰好拼出一个更大的正方形,则称其为完美正方形.历史上,人们花了很久才找到了若干完美正方形.比如:如下边长的22个正方形 2 3 4 6 7 8 12 13 ...

  7. leetcode第221题(最大正方形)的本地IDE实现及变形

    问题描述: 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积.PS:本文也对只包含0的最大正方形面积进行了运算 示例: 输入: 1 0 1 0 0 1 0 1 1 1 ...

  8. c# 制作正方形图片

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  9. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

随机推荐

  1. CoreData的基本操作

     Managed Object Model(被管理对象模型): –数据库的轮廓,或者结构.包含了各个实体的定义信息      Persistent Store Coordinator (持久性数据协调 ...

  2. Centos6.5安装glusterfs3.6.2

    硬件环境Centos6.5  glusterfs3.6.2 先安装必要的包 yum install flex bison 2. 下载glusterfs3.6.2 wget http://downloa ...

  3. How to Delete using INNER JOIN with SQL Server?

    https://stackoverflow.com/questions/16481379/how-to-delete-using-inner-join-with-sql-server You need ...

  4. tp3.2关联模型 BELONGS_TO

    <?php namespace Home\Model; use Think\Model\RelationModel; class AttenModel extends RelationModel ...

  5. HDU 2430 Beans (单调队列+公式化简)

    题意:给你n袋豆子,每袋都有w[i]个豆子,接着任选连续任意个袋子的豆子合在一起放入容量为p的多个袋子里(每个袋子必须放满),问剩余的豆子数<=k时,能放满最多的袋子的个数 题解:个数与p都比较 ...

  6. super和this 在构造方法上的用法

    super Super()表示调用父类的构造方法.如果没有定义构造方法,那么就会调用父类的无参构造方法,即super(). this 在构造方法中,this表示本类的其他构造方法:student(st ...

  7. 在eclipse中使用Github进行单元测试

    时间分配是找资料60%,动手实践40%.因为之前一直没怎么好好学习,导致看到使用Gunit进行单元测试的时候感觉像是看到了一行符号一样不知所措,还好现在搜索引擎很强大,找资料学习中间有找到一个现成的傻 ...

  8. Java -- JDBC 事务处理, 事务的隔离级别 脏读 不可重复读 等...

    1. 事务指逻辑上的一组操作,组成这组操作的各个单元,要不全部成功,要不全部不成功. 数据库开启事务命令 •start transaction 开启事务 •Rollback  回滚事务 •Commit ...

  9. QT 利用ListWidget 和 StackedLayout 配合实现 分页 选项

    1. 如图, 左边为listwidget,右边为StackedLayout, 通过listwidget的不同选项,可以使右边的不同页显示出来. 2. dialog.h #ifndef DIALOG_H ...

  10. hdoj1004--Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...