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. dfs的返回条件

    用到dfs时要注意设置函数的返回条件,否则会导致一直wa!!!!!

  2. SQL Server 利用WITH AS递归获取层级关系数据

    WITH AS短语,也叫做子查询部分(subquery factoring),在SQL Server 2005中提供了一种解决方案,这就是公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性 ...

  3. Pytorch的gather用法理解

    先放一张表,可以看成是二维数组 行(列)索引 索引0 索引1 索引2 索引3 索引0 0 1 2 3 索引1 4 5 6 7 索引2 8 9 10 11 索引3 12 13 14 15 看一下下面例子 ...

  4. JSP DAO(Model)

    示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...

  5. 【codevs1993】草地排水(最大流)

    最近学了最大流,于是去codevs找了几道最大流裸题(这是我第一次写网络流). 题目大意:求一个图的最大流(就是这样的裸题) 第一次A网络流的题,发个博客纪念一下. var n,m,i,j,k,h,t ...

  6. window7 共享wifi(不通过wifi软件)

    1.新建共享网络账号 管理员登录cmd输入:netsh wlan set hostednetwork mode=allow ssid=4Gtest key=12345678 ssid是无线网络名称.k ...

  7. java深入探究11-基础加强

    1. ? extends String:String 子类;? super String:String 父类 2.反射->参数化类型表示 ParameteredType:参数化类型表示,就是获得 ...

  8. Qt之密码框不可全选、复制、粘贴无右键菜单等

    转载---> http://blog.sina.com.cn/s/blog_a6fb6cc90101artk.html 在做用户登录界面的时候,往往会用到密码框,则其中的一些功能也要求与普通的输 ...

  9. 对象存储API

    使用对象存储API步骤: 1.购买腾讯云对象存储(COS)服务 2.在腾讯云 对象存储控制台 里创建一个Bucket 3.在控制器 个人API密钥 页里获取APPID,SecretID,SecretK ...

  10. review07

    java类可以有两种重要的成员:成员变量和方法,实际上java还允许有一种成员:内部类.内部类是在一个类中定义另一个类.内部类和外嵌类的关系如下: (1)内部类的外嵌类的成员变量在内部类中仍然有效,内 ...