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

Anniversary Cake

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!

题意:将一个大的正方形的蛋糕恰好切成多个小正方形,问能否做到。解析:首先判断所有小正方形的面积之和是否等于大正方形,不等肯定不行。还有如果小正方形中存在两个最大的边长之和大于大正方形,肯定也不行。搜索的话,由于小正方形的边长最大为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;
}
时间: 2024-10-22 20:08:06

poj 1020 Anniversary Cake(切正方形蛋糕+搜索)的相关文章

poj 1020 Anniversary Cake (DFS)

出处: http://blog.csdn.net/lyy289065406/article/details/6683250 大致题意: 有一块边长为BoxSize的正方形的大蛋糕,现在给出n块不同尺寸的正方形的小蛋糕的边长,问是否能把大蛋糕按恰好切割为这n块小蛋糕,要求每块小蛋糕必须为整块. 解题思路: 有技巧的DFS 可以把大蛋糕想象为一个蛋糕盒子,然后往里面装小蛋糕. 装蛋糕时遵循以下原则: 自下而上,自左至右: 即先装好盒子底部,再继续往上层装,且装每一层时都靠左边放蛋糕: 大蛋糕优先装,

poj 1020 Anniversary Cake dfs的灵活结构

题意: 给一个目标正方形边长和n个小正方形的边长,问是否可以用这n个小正方形填满目标正方形. 分析: dfs不一开始就定好放入顺序,而是用已放入的个数代表深搜维度. 代码: #include <iostream> using namespace std; int box_size; int n; int size_num[16]; int col[64]; bool dfs(int cnt) { if(cnt==n) return true; int minx=INT_MAX,col_inde

POJ1020 Anniversary Cake

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16683   Accepted: 5442 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 wit

【DFS】Anniversary Cake

[poj1020]Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17203   Accepted: 5619 Description Nahid Khaleh decides to invite the kids of the "Shahr-e Ghashang" to her wedding anniversary. She wants to prepare a square

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 wit

poj 2324 Anniversary party(树形DP)

/*poj 2324 Anniversary party(树形DP) ---用dp[i][1]表示以i为根的子树节点i要去的最大欢乐值,用dp[i][0]表示以i为根节点的子树i不去时的最大欢乐值, ---于是当i去时,i的所有儿子都不能去:dp[i][1]=sum(dp[j][0])+a[i],其中j是i的儿子节点. ---当i不去时,i的儿子可去也可不去:dp[i][0]=sum(max(dp[j][0],dp[j][1])),j是i的儿子节点 ---边界条件:当i时叶子节点时,dp[i][

poj 3087 Shuffle&#39;m Up (模拟搜索)

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5953   Accepted: 2796 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of

POJ 1979 Red and Black【深度优先搜索】

题目链接:http://poj.org/problem?id=1979 题目大意:一个矩形的房间地板被分为w*h个小块,每一个小块不是红的就是黑的,你首先站在一个黑色小块上,你只能朝你的四个方向(上下左右)移动,且不能到达红色的小块上,问你最多能到达多少个小块. 很简单的dfs深度优先搜索 没搜索过一个格子,将该格子设置为红色,之后的搜索就不会再搜索到该格子,就不会造成重复,因为该题有很多数据,记得每次处理数据是初始化各数组及其他数据. 代码如下: #include <iostream> #i

poj 2342 Anniversary party,树形DP easy

poj 2342 Anniversary party 没有上司的晚会 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数最大.但是,没有职员愿和直接上司一起与会. 程序名:party 输入格式: 第一行一个整数N.(1<=N<=6000) 接下来N行,第i+1行表示i号职员的快乐指数Ri.(-128<=Ri<=127) 接下来N-1行,每行输入