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 ...
随机推荐
- 【hihocoder#1388】Periodic Signal NTT
题目链接:http://hihocoder.com/problemset/problem/1388?sid=974337 题目大意:找出一个$k$,使得$\sum_{i=0}^{n-1}(A_{i}- ...
- c# 大量拼接xml时内存溢出解决方法
public static string SelectUNnormalPriceSTrans(EUNnormalPriceS rqInfo) { string guidStrJianJclFirst ...
- mongodb 基本语法
成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...
- hdu 1245 Saving James Bond
http://acm.hdu.edu.cn/showproblem.php?pid=1245 #include <cstdio> #include <cstring> #inc ...
- BZOJ 1502 月下柠檬树(simpson积分)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1502 题意:给出如下一棵分层的树,给出每层的高度和每个面的半径.光线是平行的,与地面夹角 ...
- Qt如何去掉按钮等控件的虚线框(焦点框)(两种方法)
方法1:可以通过代码ui->pushButton->setFocusPolicy(Qt::NoFocus)或在Qt Creator的属性列表中设置. 方法2:如果在嵌入式设备中需要通过按键 ...
- android:duplicateParentState属性解释
android:duplicateParentState指的是当前控件是否跟随父控件的(点击.焦点等)状态 例:假设一Layout有两子View,对Layout进行监听点击事件:子ViewA一个设置d ...
- jquery第六期:位置选择器
<html> <head> <script type="text/javascript" src="jquery-1.10.1.js&quo ...
- 福建省队集训被虐记——DAY3
昨天没写--今天补上吧 一如既往的跪了 棋盘 [问题描述] 给出一个N*M的方格棋盘,每个格子里有一盏灯和一个开关,开始的时候,所有的灯都是关着的.用(x, y)表示第x行,y列的格子.(x, y)的 ...
- Socket,非阻塞,fcntl
一.fcntl 用以下方法将socket设置成为非阻塞方式 int flags = fcntl(socket,F_GETFL,0); fcntl(socket,F_SETFL,flags|O_NON ...