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. Java中Arrays.sort()和Collections.sort()

    1.简单示例 sort方法的使用非常的简单明了,下面的例子中,先定义一个比较Dog大小的Comparator,然后将其实例对象作为参数传给sort方法,通过此示例,你应该能够快速掌握Arrays.so ...

  2. iOS上架被拒原因及解决办法

    简单的记录一下,近期APP上架所遇到的坑爹事儿吧!! 第一次提交: 第二天给了回复,内容如下: .Guideline - Performance - Software Requirements You ...

  3. Android修改init.rc和init.xx.rc文件【转】

    本文转载自:https://blog.csdn.net/u013686019/article/details/47981249 一.文件简介 init.rc:Android在启动过程中读取的启动脚本文 ...

  4. c# 图片 与 BASE64 字符串 互相转换。

    using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System. ...

  5. IDEA中集成JRebel插件

    下载下面2个插件 jr-ide-intellij-6.4.3_13-16.zip --- 官网的jar(地址:https://plugins.jetbrains.com/plugin/4441-jre ...

  6. python统计代码行数

    以前写了一个java的统计代码行数的小程序,最近在看python,于是就参考前辈的代码,写了一个统计文件夹下面各种程序的代码的小程序,这里贴出来供大家参考 参考链接: https://gist.git ...

  7. P3391 文艺平衡树

    hh 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  8. java项目 里的DAO,model,service, IMPL含义

    在一般工程中 基本上都会出现上述的字眼首先 DAO 提供了应用程序与数据库之间的操作规范 和操作 用于通常数据库的增删查改 一般如果使用框架 都是由框架自动生成,提高访问效率和便于快速开发.hiber ...

  9. JMeter常用的调试工具

    JMeter常用的调试工具有如下五种: 1.View Tree:查看结果树.含请求信息.响应信息等,请求头信息中的cookie信息一般默认不会显示,可通过修改JMeter配置参数进行显示.日常大家用的 ...

  10. SQL必知必会 记录

    登录数据库mysql -u root -p查看所有数据库  show databases:选择数据库  use 数据库名:查看所有表      show tables查看表结构      descri ...