Anniversary Cake
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 15704   Accepted: 5123

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!

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
题解:可理解为将多个正方形小蛋糕放入一个正方形蛋糕盒子。从左往右,从前往后放入。简单的DFS
 
代码:
 #include<iostream>
#include<cstring>
using namespace std;
int col[]; //将蛋糕分为1*1的小块,下标表示列,值表示用到第几行
int cakesize; //蛋糕大小
int part[]; //数组值为该大小的小块的个数
int num; //蛋糕个数 bool DFS(int fillnum) //从前往后,从左往右放入
{
int min=;
int flag;
int wide;
if(fillnum==num)
return true;
for(int i=; i<=cakesize; i++) //记录所有列里所用最少的
if(min>col[i])
{
min=col[i];
flag=i;
}
for(int size=; size>; size--) //从大到小遍历,从大的开始放,越小灵活性越大
{
if(!part[size])
continue;
if(cakesize-min>=size&&cakesize-flag+>=size) //判断蛋糕放入‘是否有可能’溢出,是否有‘可能’放入
{
wide=; //之前错在这里
for(int j=flag; j<=flag+size-; j++) //与上面的if判断一起,其作用为判断是否能放下该块蛋糕
{
if(col[j]<=min)
wide++;
else
break;
}
if(wide>=size)
{
part[size]--;
for(int k=flag; k<=flag+size-; k++)
col[k]+=size;
if(DFS(fillnum+))
return true;
part[size]++; //回溯
for(int k=flag; k<=flag+size-; k++)
col[k]-=size;
}
}
}
return false;
} int main()
{
int t,side;
cin>>t;
while(t--)
{
memset(part,,sizeof(part));
memset(col,,sizeof(col));
cin>>cakesize;
cin>>num;
for(int i=; i<=num; i++)
{
cin>>side;
part[side]++;
}
if(DFS())
cout<<"KHOOOOB!"<<endl;
else
cout<<"HUTUTU!"<<endl;
}
return ;
}

Anniversary Cake的更多相关文章

  1. POJ 1020 Anniversary Cake(DFS)

    Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...

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

                                                                                                         ...

  3. 【DFS】Anniversary Cake

    [poj1020]Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17203   Accep ...

  4. POJ1020 Anniversary Cake

    题目来源:http://poj.org/problem?id=1020 题目大意:有一块边长为s的正方形大蛋糕,有n个客人,每个客人想分一块边长为si的正方形蛋糕.求这块大蛋糕能否恰好满足所有客人的需 ...

  5. 【poj1020】 Anniversary Cake

    http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像 ...

  6. 2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest

    A. Anniversary Cake 随便挑两个点切掉就好了. #include<bits/stdc++.h> using namespace std; const int Maxn=2 ...

  7. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  8. BFS广搜题目(转载)

    BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位 ...

  9. POJ1020(小正方形铺大正方形)

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

随机推荐

  1. SSLStrip 终极版 —— location 瞒天过海

    之前介绍了 HTTPS 前端劫持 的方案,尽管非常有趣.然而现实却并不理想. 其唯一.也是最大的缺陷.就是无法阻止脚本跳转.若是没有这个缺陷,那就非常完美了 -- 当然也就没有必要写这篇文章了. 说究 ...

  2. 1.1 Spring概述

        Spring是分层的Java SE/EE应用一站式的轻量开源框架,以 反转控制(Inverse of Control,IoC).面向切面编程(Aspect Oriented Programmi ...

  3. 【面试加分项】java自己定义注解之解析注解

    我之前的博客中说明过自己定义注解的声明今天我们来看看怎样对我们自己定义的注解进行使用. 1.我们在程序中使用我们的注解. 上一篇中我们自己定义了一个注解: @Target(ElementType.FI ...

  4. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  5. Java的Graphics中drawImage与drawLine的坐标区别

    drawImage复制的区域是 dx1 <= x < dx2,dy1 <= y < dy2 drawLine绘制区域是 dx1 <= x <= dx2,dy1 &l ...

  6. 【Codeforces】665E Beautiful Subarrays

    E. Beautiful Subarrays time limit per test: 3 seconds memory limit per test: 512 megabytes input: st ...

  7. 用C语言实现websocket服务器

    Websocket Echo Server Demo 背景 嵌入式设备的应用开发大都依靠C语言来完成,我去研究如何用c语言实现websocket服务器也是为了在嵌入式设备中实现一个ip camera的 ...

  8. [译]NUnit--Installation(三)

    Installation NUnit安装程序默认安装文件路径为C:\Program Files\NUnit 2.6.2.根据用户选择安装的选项,安装文件有三个子文件夹:bin.doc.samples. ...

  9. missing required source folder

    Eclipse 中XXX is missing required source folder 问题的解决 https://blog.csdn.net/itzhangdaopin/article/det ...

  10. 解决 django 中 mysql gone away 的问题

    最近在项目中,我使用 Django Command 模块写了一个脚本,处理从 MQ 发来的消息,并入库.在测试过程中,程序运行良好,但是在程序上线并运行一段时间后,出现了以下错误: Operation ...