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. POJO、Bean和JavaBean

    本文总结自: https://blog.csdn.net/chenchunlin526/article/details/69939337 POJO (plain pld java object) 一个 ...

  2. crm销售管理系统一

    一.环境配置 1.首先配置pip,环境变量配置 pip 9.0.1 from c:\users\administrator\envs\crm\lib\site-packages (python 3.6 ...

  3. Python中的条件选择和循环语句

    一.条件选择语句 Python中条件选择语句的关键字为:if .elif .else这三个.其基本形式如下: if condition: block elif condition: block ... ...

  4. python 中文字符的处理

    刚开始学习python的时候,都是对这英文的翻译书学习的.没有解除到中文编码的相关问题,直到自己用python去做相关的项目的时候才发先中文编码问题真的非常头疼啊.这里分享一下本人所了解的一些经验. ...

  5. 一个gpio 不受控制的bug

    前几天调试一个flash灯的驱动程序,这可ic 有两个控制pin, 一个叫en1 一个叫en2, 根据spec的说明,不同的组合将产生不同的输出电流.但我发现,那个en1 这个pin 死活是拉不高的, ...

  6. 剑指Offer——圆圈中最后剩下的数(约瑟夫环)

    Question 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后, ...

  7. js获取css样式方法

    一.CSS样式共有三种:内联样式(行间样式).内部样式.外部样式(链接式和导入式) <div id="a" style="width: 100px;height: ...

  8. 派派和京东的paipai域名之争

    最近有一款叫“派派”的APP很火,微博上.电梯里.群里到处都是推广广告.不仅如此,还有大张伟.关晓彤.王祖蓝等十几个明星发帖为“派派”站台.有消息称,派派这段时间仅线上推广就花去了约1600万. 总融 ...

  9. PAT1058. A+B in Hogwarts (20)

    #include <iostream> using namespace std; int ag,as,ak; int bg,bs,bk; int cg,cs,ck; int main(){ ...

  10. QT XML文档的解析 QXmlStreamReader, DOM,SAX 三种解析方法 简单示例

    0. xml文档如下 <?xml version="1.0"?> <bookindex> <entry term="sidebearings ...