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 ...
随机推荐
- 什么是REST架构 - z
什么是REST架构 - z REST架构风格是全新的针对Web应用的开发风格,是当今世界最成功的互联网超媒体分布式系统架构,它使得人们真正理解了Http协议本来面貌.随着 REST架构成为主流技术 ...
- 【CCNA学习笔记】1.思科路由器的基本配置
教学视频来源:http://edu.51cto.com/lesson/id-10930.html. 怎么安装模块.连交叉线什么的视频里面老师说的很清楚了,我只记录一下IOS配置的命令(虽然一副不明觉厉 ...
- openssl对数组加密解密的完整实现代码
本例是用C实现的对一个数组进行加密,加密到第二个数组,然后解密到另一个数组的完整实现代码. #include <stdio.h> #include <string.h> #in ...
- Qt自定义带游标的slider,在滑块正上方显示当前值(非常有意思,继承QSlider之后增加一个QLabel,然后不断移动它)
首先自定义QSlider的子类MyCustomSlider,如下所示. mycustomslider.h #ifndef MYCUSTOMSLIDER_H #define MYCUSTOMSLIDER ...
- Powershell环境变量
Powershell环境变量 9 12月, 2011 在 Powershell tagged 变量by Mooser Lee 本文索引 [隐藏] 1读取特殊的环境变量 2查找环境变量 3创建新的环境 ...
- 【转】Android下编译jni库的二种方法(含示例)
原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从N ...
- JQuery 图片延迟加载并等比缩放插件
原文地址:http://www.shangxueba.com/jingyan/1909987.html DEMO地址:http://demo.jb51.net/html/jquery_img/jque ...
- mysql 中 LIMIT的简单用法
mysql--语法: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset --举例: select * from table l ...
- Integer to English Words 解答
Question Convert a non-negative integer to its english words representation. Given input is guarante ...
- HDU1698 Just a Hook (区间更新)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...