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. Qt编译器

    有两种,MSVC和MINGW Qt 中有两种方式编译,一种是MinGW ,另一种MSVC. MSVC是指微软的VC编译器: MingGW是指是Minimalist GNU on Windows的缩写. ...

  2. Kubernetes 命令补全

    yum install -y bash-completionsource /usr/share/bash-completion/bash_completionsource <(kubectl c ...

  3. Ubuntu 使用国内apt源

    编辑/etc/apt/source-list deb http://cn.archive.ubuntu.com/ubuntu/ trusty main restricted universe mult ...

  4. 1.1nginx安装

    1.必要软件准备 安装 pcre为了支持 rewrite 功能,我们需要安装 pcre# yum install pcre* //如过你已经装了,请跳过这一步 安装 openssl需要 ssl 的 ...

  5. 3.django连接mysql数据库及安装mysqldb驱动报错解决办法

    1.在setting.py设置连接数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djang ...

  6. springmvc异常处理(非注解与注解)

    1.异常 程序中的异常一般分为两类:预期异常,运行时异常.前者是我们可预知的,我们一般通过捕获和抛出方式处理这些异常.后者主要通过代码规范.测试等手段来减少异常的发生.一般,我们在系统的DAO.Ser ...

  7. Windows命令行乱码问题解决

    命令 chcp功能: 显示或设置活动代码页编号 CHCP [nnn] nnn 指定代码页编号. 不加参数键入 CHCP 显示活动代码页编号. nnn指定一已有的系统字符集,该字符集在CONFIG.SY ...

  8. hdu 1695 GCD 莫比乌斯

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. hadoop环境搭建(linux单机版)

    一.在Ubuntu下创建hadoop用户组合hadoop用户 1.创建hadoop用户组      addgroup hadoop 2.创建hadoop用户      adduser -ingroup ...

  10. winform里面的label怎么样实现,字上删除的效果

    label属性里的Font方法理由 Strikeout 选成True 就行了 button1_Click(object sender, EventArgs e) { Graphics g = Grap ...