poj1020 Anniversary Cake 搜索

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 X M的平面蛋糕 问是否能分成N块给定面积为a X a的蛋糕

首先应该换个思路理解题目   能否分成N块  ==  能否恰用N块填充满

然后再按常规搜索思路给出填充策略 :
1.先下后上 先左后右
2.先填充大的蛋糕再填充小的

接着开始搜索了, 直接将蛋糕理解为二维数组去搜索是不行的耗时而且麻烦
我们可以给出一个数组 column[N] 表示每列填充了几个1X1的小蛋糕  要判断
能否填充一个a X a蛋糕(假设从J列开始)  只需要判断col[j]~col[j+a-1]
是否都满足 M-col[x]>=a 即可 按上述策略搜索即可得到答案

另注意两个剪枝:
大蛋糕的面积!=所有小蛋糕的面积之和时    肯定不行
边长比大蛋糕的边长的二分之一长的小蛋糕数量大于一时肯定不行 (可画图理解) 

代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int sumsize,n;
int col[45],sizenum[12]; //MAXarea=1600    sizenum 表示这个size的数量
int dfs(int filled)
{
    if(filled==n)
        return 1;
    int i,j,minn=20;
    for(i=1; i<=sumsize; i++)   //找填充最少的列开始填充
    {
        if(col[i]<minn)
        {
            minn=col[i];
            j=i;
        }
    }
    for(int size=10;size>=1;size--)
    {
        if(sizenum[size])
        {
            if(sumsize-minn>=size&&sumsize+1-j>=size)
            {
                int wide=0;
                for(i=j;i<j+size;i++)  //判断横方向能否填充
                {
                    if(col[i]<=minn)
                    {
                        wide++;
                        continue;
                    }
                    break;
                }
                if(wide>=size)
                {
                    sizenum[size]--;        //填充
                    for(i=j;i<j+size;i++)
                        col[i]+=size;

                    if(dfs(filled+1))
                        return 1;

                    sizenum[size]++;         //回溯
                    for(i=j;i<j+size;i++)
                        col[i]-=size;
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    int size;
    int area,s;
    scanf("%d",&t);
    while(t--)
    {
        s=area=0;
        memset(sizenum,0,sizeof(sizenum));
        memset(col,0,sizeof(col));
        scanf("%d%d",&sumsize,&n);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&size);
            sizenum[size]++;
            if(size>sumsize/2)
                s++;
            area+=size*size;
        }
        if(s>1||area!=sumsize*sumsize)       //两个剪枝
        {
            cout<<"HUTUTU!"<<endl;
            continue;
        }
        else
        {
            if(dfs(0))
                cout<<"KHOOOOB!"<<endl;
            else
                cout<<"HUTUTU!"<<endl;
        }
    }
    return 0;
}

时间: 2024-10-08 09:37:36

poj1020 Anniversary Cake 搜索的相关文章

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

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 o

【poj1020】 Anniversary Cake

http://poj.org/problem?id=1020 (题目链接) 题意 有一个S*S的大蛋糕,还有许多正方形的小蛋糕,问能否将大蛋糕完整的分成所有的小蛋糕,不能有剩余. Solution 像这种看起来很麻烦的基本上都是水题,然而poj上的所谓水题,我也是呵呵了. 在根据题意做完若干判断剪枝后,我们开始搜索.因为蛋糕不能有剩余,所以搜索就很好搜了,刚开始没注意,不知道不允许剩余,直接参考了别人的程序= =.详情请见:题解 代码 // poj1020 #include<algorithm>

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 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

poj 1020 Anniversary Cake (DFS)

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

wewe

BFS广搜题目有时间一个个做下来 2009-12-29 15:09 1574人阅读 评论(1) 收藏 举报 图形graphc优化存储游戏 有时间要去做做这些题目,所以从他人空间copy过来了,谢谢那位大虾啦. pku 1175 Starry Night 题目地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1175 解法:BFS,要注意的是如何判断图形是一样的,我的做法就是计算每两个点的距离之和. 看:http://hi.baidu.com/doxi

POJ刷题

这篇文章会持续更新, 记录我所有AC的POJ题目. PS:我所有的POJ代码都存在我的github上. 1000 A+B 水题不说. 1001 Exponentiation 求一个数的n次方,用高精度,注意细节. 1002 487-3279 题目描述:设计程序,按照功能机上的9键键位把字母电话号码转化成数字电话号码,并将电话号码格式化(原本的电话号码格式里可能出现无数个短横线-(=_=#)) 对于给定输入,输出有重复的电话号码,并给出重复的次数. 输入格式:第一行一整数n,表示电话号码的数量,接