POJ 1020 Anniversary Cake(DFS)
Anniversary Cake
Time Limit: 1000MS
Memory Limit: 10000KB
64bit IO Format: %I64d & %I64u
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!
题目简单翻译:
给你一个方形蛋糕,问能不能把它切成m个小蛋糕,而不浪费。
解题思路:
主要思路来源于優YoU,大致是从下往上填格子。
代码:
#include<iostream>
#include<cstring>
using namespace std; int BoxSize;
int n;
int SizeNum[];
int col[]; bool DFS(int FillNum)
{
if(FillNum==n) return true;
int min=;
int prow;
for(int i=;i<=BoxSize;i++)
if(min>col[i])
{
min=col[i];
prow=i;
}
for(int size=;size>=;size--)
{
if(!SizeNum[size]) continue;
if(BoxSize-col[prow]>=size&&BoxSize-prow+>=size)
{
int wide=;
for(int r=prow;r<=prow+size-;r++)
{
if(col[r]<=col[prow])
{
wide++;
continue;
}
break;
} if(wide>=size)
{
int r;
SizeNum[size]--;
for(r=prow;r<=prow+size-;r++)
col[r]+=size;
if(DFS(FillNum+)) return true;
SizeNum[size]++;
for(r=prow;r<=prow+size-;r++)
col[r]-=size;
}
} }
return false;
} int main(void)
{
int test;
cin >> test;
for(int t=;t<=test;t++)
{
memset(SizeNum,,sizeof SizeNum);
memset(col,,sizeof col);
cin >> BoxSize >> n;
int cnt=,area=;
for(int i=;i<=n;i++)
{
int size;
cin >> size;
area+=size*size;
SizeNum[size]++;
if(size>BoxSize/) cnt++;
}
if(cnt>||area!=BoxSize*BoxSize)
{
cout << "HUTUTU!" << endl;
continue;
}
if(DFS()) cout << "KHOOOOB!" << endl;
else cout << "HUTUTU!" << endl;
}
}
POJ 1020 Anniversary Cake(DFS)的更多相关文章
- poj 1020 Anniversary Cake(切正方形蛋糕+搜索)
...
- POJ 3009-Curling 2.0(DFS)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 5125 Desc ...
- 题解报告:poj 1321 棋盘问题(dfs)
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...
- POJ 2251 Dungeon Master(dfs)
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...
- [ACM] POJ 3740 Easy Finding (DFS)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16202 Accepted: 4349 Description Give ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
- POJ 1321 棋盘问题(dfs)
传送门 棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38297 Accepted: 18761 Descri ...
- POJ 1321 棋盘问题 (dfs)
在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
随机推荐
- Honkly分享链接集总篇
VC6.0 Filetool Honkly版 http://pan.baidu.com/s/1bnentr5 密码:15eq,解压密码:honkly VC6.0 Filetool 官方 ...
- DBA 思想天空笔记
/*+leading(t1) use_nl(t2*/这个HINT的含义,其中use_nl表示强制用嵌套循环连接方式.Leading(t1)表示强制先访问t1表,也就是t1表作为驱动表,增加HINT的目 ...
- BZOJ 1502 月下柠檬树(simpson积分)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1502 题意:给出如下一棵分层的树,给出每层的高度和每个面的半径.光线是平行的,与地面夹角 ...
- Qt编程之信号与槽-------unresolved external symbol "public: virtual struct QMetaObject const * __thiscall XX::metaObject(void)const
原因是加入Q_OBJECT这个macro的类,被编译的时候就要用到moc这个命令,所以在VS2010中,没有加入此命令的应用,当然会出错了.所以解决办法是加,或者如果你不使用信号槽可以直接删除. 当要 ...
- 【czy系列赛】czy的后宫6 && bzoj1044 [HAOI2008]木棍分割
题目描述 众所周知的是丧尸czy有很多妹子(虽然很多但是质量不容乐观QAQ),今天czy把n个妹子排成一行来检阅.但是czy的妹子的质量实在--所以czy看不下去了.检阅了第i个妹子会增加czy a[ ...
- Linux服务器SNMP常用OID (转)
原文地址:http://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错. 服务器负载: 1 2 ...
- hihoCoder 1092 : Have Lunch Together
题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...
- 再造轮子之网易彩票-第二季(IOS 篇 by sixleaves)
02-彩票项目第二季 2.封装SWPTabBar方式一 接着我们思考如何进行封装.前面已经将过了为什么要封装, 和封装达到的效果.这里我们主要有两种封装方式,分别是站在不同的角度上看待问题.虽然角度不 ...
- VMware+Ubuntu8.10+Skyeye+gdb实现u-boot源码调试
系统平台:WindowsXP 虚拟机: VMware Workstation 6.5.0 Ubuntu8.10 安装程序 ubuntu-8.10-desktop-i386.iso 下载地址:http: ...
- spring3 jsp页面使用<form:form modelAttribute="xxxx" action="xxxx">报错,附连接数据库的spring MVC annotation 案例
在写一个使用spring3 的form标签的例子时,一直报错,错误信息为:java.lang.IllegalStateException: Neither BindingResult nor plai ...