poj 1020 Anniversary Cake(切正方形蛋糕+搜索)
Input
Output
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! 题意:将一个大的正方形的蛋糕恰好切成多个小正方形,问能否做到。
解析:首先判断所有小正方形的面积之和是否等于大正方形,不等肯定不行。还有如果小正方形中存在两个最大的边长之和大于大正方形,肯定也不行。搜索的话,由于小正方形的边长最大为10,而且最多只有16个,因此大正方形的边长最大为40
那么我从底层开始一层层往上铺,用一个col[]数组保存这一列对应的高度,要铺的话总是找col[]中最小最左边的(铺最低的地方)还要判断能不能铺某个正方形,直到所有正方形铺完。 代码如下:
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iterator>
#include<utility>
#include<sstream>
#include<iostream>
#include<cmath>
#include<stack>
using namespace std;
const int INF=1000000007;
const double eps=0.00000001;
int edge,N;
int H[11],col[41];
bool dfs(int cur)
{
if(cur==N) return true; //铺完了
int minX=50,pos;
for(int i=1;i<=edge;i++) if(col[i]<minX) { minX=col[i],pos=i; } //找到最低最靠左的位置
for(int i=10;i>=1;i--) //枚举正方形高度
{
if(!H[i]) continue;
if(minX+i<=edge&&pos+i<=edge+1) //不能越界
{
bool ok=true;
for(int st=pos;st<pos+i;st++) //还要判断能不能铺
{
if(col[st]>col[pos]){ ok=false; break; }
}
if(ok)
{
for(int st=pos;st<pos+i;st++) col[st]+=i; //铺的位置全部加上这个高度
H[i]--; //删掉这个正方形
if(dfs(cur+1)) return true;
H[i]++;
for(int st=pos;st<pos+i;st++) col[st]-=i;
}
}
}
return false;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>edge>>N;
memset(H,0,sizeof(H));
memset(col,0,sizeof(col));
int sum=0,cnt=0;
for(int i=1;i<=N;i++)
{
int size;
cin>>size;
H[size]++;
sum+=size*size;
if(size>edge/2) cnt++;
}
if(cnt>1||edge*edge!=sum) //事先预判断
{
cout<<"HUTUTU!"<<endl;
continue;
}
if(dfs(0)) cout<<"KHOOOOB!"<<endl;
else cout<<"HUTUTU!"<<endl;
}
return 0;
}
poj 1020 Anniversary Cake(切正方形蛋糕+搜索)的更多相关文章
- POJ 1020 Anniversary Cake(DFS)
Anniversary Cake Time Limit: 1000MSMemory Limit: 10000KB64bit IO Format: %I64d & %I64u Submit St ...
- POJ1020 Anniversary Cake
题目来源:http://poj.org/problem?id=1020 题目大意:有一块边长为s的正方形大蛋糕,有n个客人,每个客人想分一块边长为si的正方形蛋糕.求这块大蛋糕能否恰好满足所有客人的需 ...
- 【DFS】Anniversary Cake
[poj1020]Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17203 Accep ...
- Anniversary Cake
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15704 Accepted: 5123 ...
- poj 2324 Anniversary party(树形DP)
/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- DP Intro - poj 2342 Anniversary party
今天开始做老师给的专辑,打开DP专辑 A题 Rebuilding Roads 直接不会了,发现是树形DP,百度了下了该题,看了老半天看不懂,想死的冲动都有了~~~~ 最后百度了下,树形DP入门,找到了 ...
- 洛谷P1528 切蛋糕 [搜索,二分答案]
题目传送门 切蛋糕 题目描述 Facer今天买了n块蛋糕,不料被信息组中球球等好吃懒做的家伙发现了,没办法,只好浪费一点来填他们的嘴巴.他答应给每个人留一口,然后量了量每个人口的大小.Facer有把刀 ...
- 【poj1020】 Anniversary Cake
http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像 ...
随机推荐
- Redis 该选择哪种持久化配置
这个标题或许会让你想起<黑客帝国>里经典的台词,你要选择蓝色药丸,还是红色药丸? Redis 是我们重度使用的一个开源软件,对它的持久化配置做一番相对深入的总结,是值得的.目前它有两种主流 ...
- hdu 1429 胜利大逃亡(续)(bfs+状态压缩)
Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带 ...
- pyqt字符串分离开,放入列表中
string1 = ''''' the stirng Has many line In THE fIle ''' list_of_string = string1.split() print list ...
- C# 制作 仪表
以前在百度写的文档,转移到此处 前些天在做NetAnalyzer时,需要使用一个指针仪表,网上看了一下,也有人做过,但是大部分都是收费的,本着自力更生的原则,于是决定自己设计一个,今天拿出来有读者分享 ...
- VC皮肤库SkinSharp 1.0.6.6的使用
SkinSharp又称Skin#,是Windows环境下一款强大的换肤组件. SkinSharp作为换肤控件,仅仅须要在您的程序中加入一行代码,就能让您的界面焕然一新,并拥有多种主题风格和色调的动态切 ...
- CLR via C# - GC
//像背书一样,记录下吧 1.CLR分配资源 托管堆上维护着一个指针NextObjPtr.该指针表示下一个新建对象在托管堆上的位置.C#的new Object会产生IL newobj指令,NextOb ...
- MySql命令——表相关
auto_increment //自动增长 primary key(id) //指定主键 select last_insert_id();//获得添加列的主键值 create table produc ...
- vs2013 JS代码提示
1.JS提示 在Js文件头部加 /// <reference path="ext-all-dev.js" /> 要求引用的js和本js在同一目录,否则需要全部路径
- /调整button的title的位置
[bottomButton setTitleEdgeInsets:UIEdgeInsetsMake(10, -190, 10, 44)]; //上左下右 ||button.co ...
- VisualStudio2013快捷键
visual studio 2013 是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.VS 2013 中新增了很多提高 ...