深度优先+回溯(1): pku acm 1020 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.
 
思路:
这道题本质上就是把一堆小的正方形放入到大的正文形中。
算法:深度优先搜索+回溯
        把一堆小正方形按边长从大到小依次放入大正方形内。
        在大正方形中的位置是从左到右,从上到下。
 
这道题搞了好几天,思路看的是网上的,但提交一直是WA,郁闷。后来才发现是在reamins大于0时,如果所有情况都不符合时忘记返回0。
 
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

#define MAX_CAKES 11

int dfs(int s, int *height, int *cakes, int n, int remains)
{
    if (remains > 0)
    {    
        int j = 0, w, h;
        for (int i = 1; i < s; ++i) //取得最小高度
        {
            if (height[i] < height[j])
                j =i;
        }

        for (w = j+1; w < s; ++w)
        {
            if (height[w] > height[j])break;            
        }

        w = w - j; //可以放入的宽度

        h = s - height[j]; //可以放入的高度


        for (int i = n; i >= 1; --i)
        {
            if (cakes[i] == 0)continue;
            if (i <= w && i <= h)//放得下
            {
                cakes[i]--;
                for (int t = j; t < j + i; ++t)
                {
                    height[t] += i;
                }

                if (dfs(s, height, cakes, n, remains - 1))return 1;
                
                for (int t = j; t < j + i; ++t)
                {
                    height[t] -= i;
                }
                cakes[i]++;
            }
        }
        return 0; //very important
    }
    else
    {        
        for (int i = 0; i < s; ++i)        
            if (height[i] != s)return 0;            
        return 1;
    }

}

int main()
{
    freopen("in.txt","rt",stdin);
    //time_t start,end;
    //time (&start);

    int t;
    int s, //s, the side of the cake
        n, // the number of cake pieces
        cakes[MAX_CAKES+1],// the side of each piece
        temp;
    int height[41]; //store the current height of every column
    cin>>t;
    while (t--)
    {
        cin>>s>>n;
        memset(cakes, 0, sizeof(int)*(MAX_CAKES+1));
        for (int i = 0; i < n; ++i)
        {
            cin>>temp;
            cakes[temp] += 1;
            cakes[0] += temp*temp;
        }

        if (s*s != cakes[0]){
            cout<<"HUTUTU!"<<endl;
            continue;
        }

        //int *height = new int[s];
        memset(height, 0, sizeof(int)*41);    

        if (dfs(s, height, cakes, MAX_CAKES+1, n))
            cout<<"KHOOOOB!"<<endl;
        else
            cout<<"HUTUTU!"<<endl;

        //delete []height;
    }

    //time (&end);
    //cout<<"It took you "<<difftime (end,start)<<" seconds"<<endl;

    return 0;
}

 

 
 

转载于:https://www.cnblogs.com/dachengxu/archive/2012/12/12/2814127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值